#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 300000, M = 1000;
uint64_t f[2][N], s[M], p[M];
template <typename T>
T gauss(T n) { return (n * (n + 1)) / 2; }
template <typename T>
T range_gauss(T a, T b)
{
return gauss(b) - (a ? gauss(a - 1) : 0);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, q;
cin >> n >> q;
for (size_t i = 0; i < n; ++i)
{
size_t a;
cin >> a;
s[a - 1]++;
}
sort(s, s + M);
partial_sum(s, s + M, p);
memset(f, 255, sizeof f);
f[0][0] = 0;
for (size_t j = 0; j < M; ++j)
{
size_t const u = j & 1, v = (j & 1) ^ 1;
memset(f[v], 255, sizeof f[v]);
for (size_t i = 0; i <= (j ? p[j - 1] : 0); ++i)
if (f[u][i] != UINT64_MAX)
{
// Extend left
uint64_t l = i, r = l + s[j] - 1;
f[v][i + s[j]] = min(f[v][i + s[j]], f[u][i] + (r + 1) * n - l * l - range_gauss(l, r));
// Extend right
r = n - ((j ? p[j - 1] : 0) - i) - 1, l = r - s[j] + 1;
f[v][i] = min(f[v][i], f[u][i] + (r + 1) * n - l * l - range_gauss(l, r));
}
}
uint64_t ans = UINT64_MAX;
for (size_t i = 0; i < n; ++i)
ans = min(ans, f[M & 1][i]);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4948 KB |
Output is correct |
2 |
Correct |
41 ms |
5016 KB |
Output is correct |
3 |
Correct |
40 ms |
4936 KB |
Output is correct |
4 |
Correct |
40 ms |
4948 KB |
Output is correct |
5 |
Correct |
40 ms |
5012 KB |
Output is correct |
6 |
Correct |
39 ms |
5016 KB |
Output is correct |
7 |
Correct |
40 ms |
5016 KB |
Output is correct |
8 |
Correct |
40 ms |
4948 KB |
Output is correct |
9 |
Correct |
41 ms |
5016 KB |
Output is correct |
10 |
Correct |
40 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4948 KB |
Output is correct |
2 |
Correct |
40 ms |
4948 KB |
Output is correct |
3 |
Correct |
41 ms |
5036 KB |
Output is correct |
4 |
Correct |
43 ms |
5204 KB |
Output is correct |
5 |
Correct |
47 ms |
5332 KB |
Output is correct |
6 |
Correct |
53 ms |
5612 KB |
Output is correct |
7 |
Correct |
51 ms |
5624 KB |
Output is correct |
8 |
Correct |
56 ms |
5640 KB |
Output is correct |
9 |
Correct |
57 ms |
5644 KB |
Output is correct |
10 |
Correct |
51 ms |
5680 KB |
Output is correct |
11 |
Correct |
54 ms |
5568 KB |
Output is correct |
12 |
Correct |
51 ms |
5688 KB |
Output is correct |
13 |
Correct |
52 ms |
5564 KB |
Output is correct |
14 |
Correct |
51 ms |
5580 KB |
Output is correct |
15 |
Correct |
51 ms |
5652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4948 KB |
Output is correct |
2 |
Correct |
40 ms |
4948 KB |
Output is correct |
3 |
Correct |
41 ms |
5036 KB |
Output is correct |
4 |
Correct |
43 ms |
5204 KB |
Output is correct |
5 |
Correct |
47 ms |
5332 KB |
Output is correct |
6 |
Correct |
53 ms |
5612 KB |
Output is correct |
7 |
Correct |
51 ms |
5624 KB |
Output is correct |
8 |
Correct |
56 ms |
5640 KB |
Output is correct |
9 |
Correct |
57 ms |
5644 KB |
Output is correct |
10 |
Correct |
51 ms |
5680 KB |
Output is correct |
11 |
Correct |
54 ms |
5568 KB |
Output is correct |
12 |
Correct |
51 ms |
5688 KB |
Output is correct |
13 |
Correct |
52 ms |
5564 KB |
Output is correct |
14 |
Correct |
51 ms |
5580 KB |
Output is correct |
15 |
Correct |
51 ms |
5652 KB |
Output is correct |
16 |
Correct |
40 ms |
4948 KB |
Output is correct |
17 |
Correct |
40 ms |
5016 KB |
Output is correct |
18 |
Correct |
40 ms |
5044 KB |
Output is correct |
19 |
Correct |
46 ms |
5288 KB |
Output is correct |
20 |
Correct |
52 ms |
5548 KB |
Output is correct |
21 |
Correct |
57 ms |
5844 KB |
Output is correct |
22 |
Correct |
57 ms |
5812 KB |
Output is correct |
23 |
Correct |
59 ms |
5820 KB |
Output is correct |
24 |
Correct |
59 ms |
5804 KB |
Output is correct |
25 |
Correct |
58 ms |
5716 KB |
Output is correct |
26 |
Correct |
58 ms |
5720 KB |
Output is correct |
27 |
Correct |
59 ms |
5716 KB |
Output is correct |
28 |
Correct |
57 ms |
5716 KB |
Output is correct |
29 |
Correct |
58 ms |
5696 KB |
Output is correct |
30 |
Correct |
59 ms |
5820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4948 KB |
Output is correct |
2 |
Correct |
40 ms |
4948 KB |
Output is correct |
3 |
Correct |
41 ms |
5036 KB |
Output is correct |
4 |
Correct |
43 ms |
5204 KB |
Output is correct |
5 |
Correct |
47 ms |
5332 KB |
Output is correct |
6 |
Correct |
53 ms |
5612 KB |
Output is correct |
7 |
Correct |
51 ms |
5624 KB |
Output is correct |
8 |
Correct |
56 ms |
5640 KB |
Output is correct |
9 |
Correct |
57 ms |
5644 KB |
Output is correct |
10 |
Correct |
51 ms |
5680 KB |
Output is correct |
11 |
Correct |
54 ms |
5568 KB |
Output is correct |
12 |
Correct |
51 ms |
5688 KB |
Output is correct |
13 |
Correct |
52 ms |
5564 KB |
Output is correct |
14 |
Correct |
51 ms |
5580 KB |
Output is correct |
15 |
Correct |
51 ms |
5652 KB |
Output is correct |
16 |
Correct |
40 ms |
4948 KB |
Output is correct |
17 |
Correct |
40 ms |
5016 KB |
Output is correct |
18 |
Correct |
40 ms |
5044 KB |
Output is correct |
19 |
Correct |
46 ms |
5288 KB |
Output is correct |
20 |
Correct |
52 ms |
5548 KB |
Output is correct |
21 |
Correct |
57 ms |
5844 KB |
Output is correct |
22 |
Correct |
57 ms |
5812 KB |
Output is correct |
23 |
Correct |
59 ms |
5820 KB |
Output is correct |
24 |
Correct |
59 ms |
5804 KB |
Output is correct |
25 |
Correct |
58 ms |
5716 KB |
Output is correct |
26 |
Correct |
58 ms |
5720 KB |
Output is correct |
27 |
Correct |
59 ms |
5716 KB |
Output is correct |
28 |
Correct |
57 ms |
5716 KB |
Output is correct |
29 |
Correct |
58 ms |
5696 KB |
Output is correct |
30 |
Correct |
59 ms |
5820 KB |
Output is correct |
31 |
Correct |
40 ms |
4948 KB |
Output is correct |
32 |
Correct |
40 ms |
4948 KB |
Output is correct |
33 |
Correct |
39 ms |
5016 KB |
Output is correct |
34 |
Correct |
41 ms |
4948 KB |
Output is correct |
35 |
Correct |
43 ms |
4948 KB |
Output is correct |
36 |
Correct |
52 ms |
5060 KB |
Output is correct |
37 |
Correct |
72 ms |
5204 KB |
Output is correct |
38 |
Correct |
106 ms |
5196 KB |
Output is correct |
39 |
Correct |
163 ms |
5396 KB |
Output is correct |
40 |
Correct |
284 ms |
5716 KB |
Output is correct |
41 |
Correct |
421 ms |
6204 KB |
Output is correct |
42 |
Correct |
418 ms |
6164 KB |
Output is correct |
43 |
Correct |
420 ms |
6196 KB |
Output is correct |
44 |
Correct |
407 ms |
6088 KB |
Output is correct |
45 |
Correct |
418 ms |
6204 KB |
Output is correct |
46 |
Correct |
410 ms |
6100 KB |
Output is correct |
47 |
Correct |
420 ms |
6168 KB |
Output is correct |
48 |
Correct |
404 ms |
6088 KB |
Output is correct |
49 |
Correct |
412 ms |
6164 KB |
Output is correct |
50 |
Correct |
399 ms |
6100 KB |
Output is correct |
51 |
Correct |
409 ms |
6168 KB |
Output is correct |
52 |
Correct |
408 ms |
6156 KB |
Output is correct |
53 |
Correct |
415 ms |
6168 KB |
Output is correct |
54 |
Correct |
406 ms |
6220 KB |
Output is correct |
55 |
Correct |
407 ms |
6092 KB |
Output is correct |
56 |
Correct |
439 ms |
6164 KB |
Output is correct |
57 |
Correct |
411 ms |
6160 KB |
Output is correct |
58 |
Correct |
404 ms |
6168 KB |
Output is correct |
59 |
Correct |
402 ms |
6168 KB |
Output is correct |
60 |
Correct |
408 ms |
6160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4948 KB |
Output is correct |
2 |
Correct |
41 ms |
5016 KB |
Output is correct |
3 |
Correct |
40 ms |
4936 KB |
Output is correct |
4 |
Correct |
40 ms |
4948 KB |
Output is correct |
5 |
Correct |
40 ms |
5012 KB |
Output is correct |
6 |
Correct |
39 ms |
5016 KB |
Output is correct |
7 |
Correct |
40 ms |
5016 KB |
Output is correct |
8 |
Correct |
40 ms |
4948 KB |
Output is correct |
9 |
Correct |
41 ms |
5016 KB |
Output is correct |
10 |
Correct |
40 ms |
4948 KB |
Output is correct |
11 |
Correct |
39 ms |
4948 KB |
Output is correct |
12 |
Correct |
40 ms |
4948 KB |
Output is correct |
13 |
Correct |
41 ms |
5036 KB |
Output is correct |
14 |
Correct |
43 ms |
5204 KB |
Output is correct |
15 |
Correct |
47 ms |
5332 KB |
Output is correct |
16 |
Correct |
53 ms |
5612 KB |
Output is correct |
17 |
Correct |
51 ms |
5624 KB |
Output is correct |
18 |
Correct |
56 ms |
5640 KB |
Output is correct |
19 |
Correct |
57 ms |
5644 KB |
Output is correct |
20 |
Correct |
51 ms |
5680 KB |
Output is correct |
21 |
Correct |
54 ms |
5568 KB |
Output is correct |
22 |
Correct |
51 ms |
5688 KB |
Output is correct |
23 |
Correct |
52 ms |
5564 KB |
Output is correct |
24 |
Correct |
51 ms |
5580 KB |
Output is correct |
25 |
Correct |
51 ms |
5652 KB |
Output is correct |
26 |
Correct |
40 ms |
4948 KB |
Output is correct |
27 |
Correct |
40 ms |
5016 KB |
Output is correct |
28 |
Correct |
40 ms |
5044 KB |
Output is correct |
29 |
Correct |
46 ms |
5288 KB |
Output is correct |
30 |
Correct |
52 ms |
5548 KB |
Output is correct |
31 |
Correct |
57 ms |
5844 KB |
Output is correct |
32 |
Correct |
57 ms |
5812 KB |
Output is correct |
33 |
Correct |
59 ms |
5820 KB |
Output is correct |
34 |
Correct |
59 ms |
5804 KB |
Output is correct |
35 |
Correct |
58 ms |
5716 KB |
Output is correct |
36 |
Correct |
58 ms |
5720 KB |
Output is correct |
37 |
Correct |
59 ms |
5716 KB |
Output is correct |
38 |
Correct |
57 ms |
5716 KB |
Output is correct |
39 |
Correct |
58 ms |
5696 KB |
Output is correct |
40 |
Correct |
59 ms |
5820 KB |
Output is correct |
41 |
Correct |
40 ms |
4948 KB |
Output is correct |
42 |
Correct |
40 ms |
4948 KB |
Output is correct |
43 |
Correct |
39 ms |
5016 KB |
Output is correct |
44 |
Correct |
41 ms |
4948 KB |
Output is correct |
45 |
Correct |
43 ms |
4948 KB |
Output is correct |
46 |
Correct |
52 ms |
5060 KB |
Output is correct |
47 |
Correct |
72 ms |
5204 KB |
Output is correct |
48 |
Correct |
106 ms |
5196 KB |
Output is correct |
49 |
Correct |
163 ms |
5396 KB |
Output is correct |
50 |
Correct |
284 ms |
5716 KB |
Output is correct |
51 |
Correct |
421 ms |
6204 KB |
Output is correct |
52 |
Correct |
418 ms |
6164 KB |
Output is correct |
53 |
Correct |
420 ms |
6196 KB |
Output is correct |
54 |
Correct |
407 ms |
6088 KB |
Output is correct |
55 |
Correct |
418 ms |
6204 KB |
Output is correct |
56 |
Correct |
410 ms |
6100 KB |
Output is correct |
57 |
Correct |
420 ms |
6168 KB |
Output is correct |
58 |
Correct |
404 ms |
6088 KB |
Output is correct |
59 |
Correct |
412 ms |
6164 KB |
Output is correct |
60 |
Correct |
399 ms |
6100 KB |
Output is correct |
61 |
Correct |
409 ms |
6168 KB |
Output is correct |
62 |
Correct |
408 ms |
6156 KB |
Output is correct |
63 |
Correct |
415 ms |
6168 KB |
Output is correct |
64 |
Correct |
406 ms |
6220 KB |
Output is correct |
65 |
Correct |
407 ms |
6092 KB |
Output is correct |
66 |
Correct |
439 ms |
6164 KB |
Output is correct |
67 |
Correct |
411 ms |
6160 KB |
Output is correct |
68 |
Correct |
404 ms |
6168 KB |
Output is correct |
69 |
Correct |
402 ms |
6168 KB |
Output is correct |
70 |
Correct |
408 ms |
6160 KB |
Output is correct |
71 |
Incorrect |
72 ms |
5204 KB |
Output isn't correct |
72 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4948 KB |
Output is correct |
2 |
Correct |
41 ms |
5016 KB |
Output is correct |
3 |
Correct |
40 ms |
4936 KB |
Output is correct |
4 |
Correct |
40 ms |
4948 KB |
Output is correct |
5 |
Correct |
40 ms |
5012 KB |
Output is correct |
6 |
Correct |
39 ms |
5016 KB |
Output is correct |
7 |
Correct |
40 ms |
5016 KB |
Output is correct |
8 |
Correct |
40 ms |
4948 KB |
Output is correct |
9 |
Correct |
41 ms |
5016 KB |
Output is correct |
10 |
Correct |
40 ms |
4948 KB |
Output is correct |
11 |
Correct |
39 ms |
4948 KB |
Output is correct |
12 |
Correct |
40 ms |
4948 KB |
Output is correct |
13 |
Correct |
41 ms |
5036 KB |
Output is correct |
14 |
Correct |
43 ms |
5204 KB |
Output is correct |
15 |
Correct |
47 ms |
5332 KB |
Output is correct |
16 |
Correct |
53 ms |
5612 KB |
Output is correct |
17 |
Correct |
51 ms |
5624 KB |
Output is correct |
18 |
Correct |
56 ms |
5640 KB |
Output is correct |
19 |
Correct |
57 ms |
5644 KB |
Output is correct |
20 |
Correct |
51 ms |
5680 KB |
Output is correct |
21 |
Correct |
54 ms |
5568 KB |
Output is correct |
22 |
Correct |
51 ms |
5688 KB |
Output is correct |
23 |
Correct |
52 ms |
5564 KB |
Output is correct |
24 |
Correct |
51 ms |
5580 KB |
Output is correct |
25 |
Correct |
51 ms |
5652 KB |
Output is correct |
26 |
Correct |
40 ms |
4948 KB |
Output is correct |
27 |
Correct |
40 ms |
5016 KB |
Output is correct |
28 |
Correct |
40 ms |
5044 KB |
Output is correct |
29 |
Correct |
46 ms |
5288 KB |
Output is correct |
30 |
Correct |
52 ms |
5548 KB |
Output is correct |
31 |
Correct |
57 ms |
5844 KB |
Output is correct |
32 |
Correct |
57 ms |
5812 KB |
Output is correct |
33 |
Correct |
59 ms |
5820 KB |
Output is correct |
34 |
Correct |
59 ms |
5804 KB |
Output is correct |
35 |
Correct |
58 ms |
5716 KB |
Output is correct |
36 |
Correct |
58 ms |
5720 KB |
Output is correct |
37 |
Correct |
59 ms |
5716 KB |
Output is correct |
38 |
Correct |
57 ms |
5716 KB |
Output is correct |
39 |
Correct |
58 ms |
5696 KB |
Output is correct |
40 |
Correct |
59 ms |
5820 KB |
Output is correct |
41 |
Correct |
40 ms |
4948 KB |
Output is correct |
42 |
Correct |
40 ms |
4948 KB |
Output is correct |
43 |
Correct |
39 ms |
5016 KB |
Output is correct |
44 |
Correct |
41 ms |
4948 KB |
Output is correct |
45 |
Correct |
43 ms |
4948 KB |
Output is correct |
46 |
Correct |
52 ms |
5060 KB |
Output is correct |
47 |
Correct |
72 ms |
5204 KB |
Output is correct |
48 |
Correct |
106 ms |
5196 KB |
Output is correct |
49 |
Correct |
163 ms |
5396 KB |
Output is correct |
50 |
Correct |
284 ms |
5716 KB |
Output is correct |
51 |
Correct |
421 ms |
6204 KB |
Output is correct |
52 |
Correct |
418 ms |
6164 KB |
Output is correct |
53 |
Correct |
420 ms |
6196 KB |
Output is correct |
54 |
Correct |
407 ms |
6088 KB |
Output is correct |
55 |
Correct |
418 ms |
6204 KB |
Output is correct |
56 |
Correct |
410 ms |
6100 KB |
Output is correct |
57 |
Correct |
420 ms |
6168 KB |
Output is correct |
58 |
Correct |
404 ms |
6088 KB |
Output is correct |
59 |
Correct |
412 ms |
6164 KB |
Output is correct |
60 |
Correct |
399 ms |
6100 KB |
Output is correct |
61 |
Correct |
409 ms |
6168 KB |
Output is correct |
62 |
Correct |
408 ms |
6156 KB |
Output is correct |
63 |
Correct |
415 ms |
6168 KB |
Output is correct |
64 |
Correct |
406 ms |
6220 KB |
Output is correct |
65 |
Correct |
407 ms |
6092 KB |
Output is correct |
66 |
Correct |
439 ms |
6164 KB |
Output is correct |
67 |
Correct |
411 ms |
6160 KB |
Output is correct |
68 |
Correct |
404 ms |
6168 KB |
Output is correct |
69 |
Correct |
402 ms |
6168 KB |
Output is correct |
70 |
Correct |
408 ms |
6160 KB |
Output is correct |
71 |
Incorrect |
72 ms |
5204 KB |
Output isn't correct |
72 |
Halted |
0 ms |
0 KB |
- |