Submission #905176

# Submission time Handle Problem Language Result Execution time Memory
905176 2024-01-12T17:16:51 Z rainboy GCD-sum (CPSPC17_gcds) C
100 / 100
231 ms 27804 KB
#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

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
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4524 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4696 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4524 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4456 KB Output is correct
12 Correct 1 ms 4964 KB Output is correct
13 Correct 1 ms 4456 KB Output is correct
14 Correct 1 ms 4456 KB Output is correct
15 Correct 1 ms 4708 KB Output is correct
16 Correct 1 ms 4708 KB Output is correct
17 Correct 1 ms 4456 KB Output is correct
18 Correct 1 ms 4456 KB Output is correct
19 Correct 1 ms 4456 KB Output is correct
20 Correct 1 ms 4456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4568 KB Output is correct
4 Correct 1 ms 4456 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4532 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4524 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4524 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4696 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4524 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 4444 KB Output is correct
35 Correct 1 ms 4444 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 1 ms 4444 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4444 KB Output is correct
40 Correct 1 ms 4444 KB Output is correct
41 Correct 1 ms 4456 KB Output is correct
42 Correct 1 ms 4964 KB Output is correct
43 Correct 1 ms 4456 KB Output is correct
44 Correct 1 ms 4456 KB Output is correct
45 Correct 1 ms 4708 KB Output is correct
46 Correct 1 ms 4708 KB Output is correct
47 Correct 1 ms 4456 KB Output is correct
48 Correct 1 ms 4456 KB Output is correct
49 Correct 1 ms 4456 KB Output is correct
50 Correct 1 ms 4456 KB Output is correct
51 Correct 1 ms 4444 KB Output is correct
52 Correct 2 ms 4444 KB Output is correct
53 Correct 1 ms 4568 KB Output is correct
54 Correct 1 ms 4456 KB Output is correct
55 Correct 1 ms 4444 KB Output is correct
56 Correct 1 ms 4444 KB Output is correct
57 Correct 2 ms 4444 KB Output is correct
58 Correct 2 ms 4444 KB Output is correct
59 Correct 1 ms 4444 KB Output is correct
60 Correct 1 ms 4532 KB Output is correct
61 Correct 1 ms 4444 KB Output is correct
62 Correct 1 ms 4444 KB Output is correct
63 Correct 1 ms 4444 KB Output is correct
64 Correct 1 ms 4444 KB Output is correct
65 Correct 1 ms 4444 KB Output is correct
66 Correct 1 ms 4524 KB Output is correct
67 Correct 1 ms 4444 KB Output is correct
68 Correct 1 ms 4444 KB Output is correct
69 Correct 1 ms 4444 KB Output is correct
70 Correct 2 ms 4444 KB Output is correct
71 Correct 2 ms 4444 KB Output is correct
72 Correct 2 ms 4444 KB Output is correct
73 Correct 2 ms 4444 KB Output is correct
74 Correct 2 ms 4444 KB Output is correct
75 Correct 2 ms 4556 KB Output is correct
76 Correct 2 ms 4696 KB Output is correct
77 Correct 2 ms 4444 KB Output is correct
78 Correct 2 ms 4444 KB Output is correct
79 Correct 2 ms 4560 KB Output is correct
80 Correct 2 ms 4444 KB Output is correct
81 Correct 2 ms 4700 KB Output is correct
82 Correct 2 ms 4444 KB Output is correct
83 Correct 2 ms 4444 KB Output is correct
84 Correct 2 ms 4444 KB Output is correct
85 Correct 2 ms 4444 KB Output is correct
86 Correct 2 ms 4444 KB Output is correct
87 Correct 1 ms 4444 KB Output is correct
88 Correct 1 ms 4444 KB Output is correct
89 Correct 1 ms 4444 KB Output is correct
90 Correct 1 ms 4532 KB Output is correct
91 Correct 2 ms 4444 KB Output is correct
92 Correct 3 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4568 KB Output is correct
4 Correct 1 ms 4456 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4532 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4524 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 177 ms 27672 KB Output is correct
23 Correct 165 ms 27776 KB Output is correct
24 Correct 160 ms 27664 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 62 ms 19460 KB Output is correct
27 Correct 164 ms 27804 KB Output is correct
28 Correct 160 ms 27732 KB Output is correct
29 Correct 156 ms 27804 KB Output is correct
30 Correct 2 ms 4444 KB Output is correct
31 Correct 62 ms 19616 KB Output is correct
32 Correct 161 ms 27796 KB Output is correct
33 Correct 161 ms 27804 KB Output is correct
34 Correct 163 ms 27796 KB Output is correct
35 Correct 1 ms 4444 KB Output is correct
36 Correct 61 ms 19612 KB Output is correct
37 Correct 231 ms 22592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4524 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4696 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4524 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 4444 KB Output is correct
35 Correct 1 ms 4444 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 1 ms 4444 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4444 KB Output is correct
40 Correct 1 ms 4444 KB Output is correct
41 Correct 1 ms 4456 KB Output is correct
42 Correct 1 ms 4964 KB Output is correct
43 Correct 1 ms 4456 KB Output is correct
44 Correct 1 ms 4456 KB Output is correct
45 Correct 1 ms 4708 KB Output is correct
46 Correct 1 ms 4708 KB Output is correct
47 Correct 1 ms 4456 KB Output is correct
48 Correct 1 ms 4456 KB Output is correct
49 Correct 1 ms 4456 KB Output is correct
50 Correct 1 ms 4456 KB Output is correct
51 Correct 1 ms 4444 KB Output is correct
52 Correct 2 ms 4444 KB Output is correct
53 Correct 1 ms 4568 KB Output is correct
54 Correct 1 ms 4456 KB Output is correct
55 Correct 1 ms 4444 KB Output is correct
56 Correct 1 ms 4444 KB Output is correct
57 Correct 2 ms 4444 KB Output is correct
58 Correct 2 ms 4444 KB Output is correct
59 Correct 1 ms 4444 KB Output is correct
60 Correct 1 ms 4532 KB Output is correct
61 Correct 1 ms 4444 KB Output is correct
62 Correct 1 ms 4444 KB Output is correct
63 Correct 1 ms 4444 KB Output is correct
64 Correct 1 ms 4444 KB Output is correct
65 Correct 1 ms 4444 KB Output is correct
66 Correct 1 ms 4524 KB Output is correct
67 Correct 1 ms 4444 KB Output is correct
68 Correct 1 ms 4444 KB Output is correct
69 Correct 1 ms 4444 KB Output is correct
70 Correct 2 ms 4444 KB Output is correct
71 Correct 2 ms 4444 KB Output is correct
72 Correct 2 ms 4444 KB Output is correct
73 Correct 2 ms 4444 KB Output is correct
74 Correct 2 ms 4444 KB Output is correct
75 Correct 2 ms 4556 KB Output is correct
76 Correct 2 ms 4696 KB Output is correct
77 Correct 2 ms 4444 KB Output is correct
78 Correct 2 ms 4444 KB Output is correct
79 Correct 2 ms 4560 KB Output is correct
80 Correct 2 ms 4444 KB Output is correct
81 Correct 2 ms 4700 KB Output is correct
82 Correct 2 ms 4444 KB Output is correct
83 Correct 2 ms 4444 KB Output is correct
84 Correct 2 ms 4444 KB Output is correct
85 Correct 2 ms 4444 KB Output is correct
86 Correct 2 ms 4444 KB Output is correct
87 Correct 1 ms 4444 KB Output is correct
88 Correct 1 ms 4444 KB Output is correct
89 Correct 1 ms 4444 KB Output is correct
90 Correct 1 ms 4532 KB Output is correct
91 Correct 2 ms 4444 KB Output is correct
92 Correct 3 ms 4444 KB Output is correct
93 Correct 177 ms 27672 KB Output is correct
94 Correct 165 ms 27776 KB Output is correct
95 Correct 160 ms 27664 KB Output is correct
96 Correct 1 ms 4444 KB Output is correct
97 Correct 62 ms 19460 KB Output is correct
98 Correct 164 ms 27804 KB Output is correct
99 Correct 160 ms 27732 KB Output is correct
100 Correct 156 ms 27804 KB Output is correct
101 Correct 2 ms 4444 KB Output is correct
102 Correct 62 ms 19616 KB Output is correct
103 Correct 161 ms 27796 KB Output is correct
104 Correct 161 ms 27804 KB Output is correct
105 Correct 163 ms 27796 KB Output is correct
106 Correct 1 ms 4444 KB Output is correct
107 Correct 61 ms 19612 KB Output is correct
108 Correct 231 ms 22592 KB Output is correct