#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,sse4")
#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 300000, M = 300000;
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;
size_t m = 0;
for (size_t i = 0; i < n; ++i)
{
size_t a;
cin >> a;
s[a - 1]++;
m = max(m, a);
}
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 |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
6 ms |
4980 KB |
Output is correct |
5 |
Correct |
13 ms |
5028 KB |
Output is correct |
6 |
Correct |
14 ms |
5032 KB |
Output is correct |
7 |
Correct |
12 ms |
5024 KB |
Output is correct |
8 |
Correct |
13 ms |
4948 KB |
Output is correct |
9 |
Correct |
14 ms |
4948 KB |
Output is correct |
10 |
Correct |
13 ms |
4948 KB |
Output is correct |
11 |
Correct |
13 ms |
4940 KB |
Output is correct |
12 |
Correct |
14 ms |
4948 KB |
Output is correct |
13 |
Correct |
15 ms |
4944 KB |
Output is correct |
14 |
Correct |
13 ms |
4948 KB |
Output is correct |
15 |
Correct |
14 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
6 ms |
4980 KB |
Output is correct |
5 |
Correct |
13 ms |
5028 KB |
Output is correct |
6 |
Correct |
14 ms |
5032 KB |
Output is correct |
7 |
Correct |
12 ms |
5024 KB |
Output is correct |
8 |
Correct |
13 ms |
4948 KB |
Output is correct |
9 |
Correct |
14 ms |
4948 KB |
Output is correct |
10 |
Correct |
13 ms |
4948 KB |
Output is correct |
11 |
Correct |
13 ms |
4940 KB |
Output is correct |
12 |
Correct |
14 ms |
4948 KB |
Output is correct |
13 |
Correct |
15 ms |
4944 KB |
Output is correct |
14 |
Correct |
13 ms |
4948 KB |
Output is correct |
15 |
Correct |
14 ms |
4944 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
8 ms |
5032 KB |
Output is correct |
20 |
Correct |
14 ms |
4948 KB |
Output is correct |
21 |
Correct |
20 ms |
5032 KB |
Output is correct |
22 |
Correct |
20 ms |
5028 KB |
Output is correct |
23 |
Correct |
23 ms |
4948 KB |
Output is correct |
24 |
Correct |
20 ms |
4944 KB |
Output is correct |
25 |
Correct |
20 ms |
4940 KB |
Output is correct |
26 |
Correct |
20 ms |
5032 KB |
Output is correct |
27 |
Correct |
20 ms |
5032 KB |
Output is correct |
28 |
Correct |
20 ms |
5036 KB |
Output is correct |
29 |
Correct |
25 ms |
4940 KB |
Output is correct |
30 |
Correct |
20 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
6 ms |
4980 KB |
Output is correct |
5 |
Correct |
13 ms |
5028 KB |
Output is correct |
6 |
Correct |
14 ms |
5032 KB |
Output is correct |
7 |
Correct |
12 ms |
5024 KB |
Output is correct |
8 |
Correct |
13 ms |
4948 KB |
Output is correct |
9 |
Correct |
14 ms |
4948 KB |
Output is correct |
10 |
Correct |
13 ms |
4948 KB |
Output is correct |
11 |
Correct |
13 ms |
4940 KB |
Output is correct |
12 |
Correct |
14 ms |
4948 KB |
Output is correct |
13 |
Correct |
15 ms |
4944 KB |
Output is correct |
14 |
Correct |
13 ms |
4948 KB |
Output is correct |
15 |
Correct |
14 ms |
4944 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
8 ms |
5032 KB |
Output is correct |
20 |
Correct |
14 ms |
4948 KB |
Output is correct |
21 |
Correct |
20 ms |
5032 KB |
Output is correct |
22 |
Correct |
20 ms |
5028 KB |
Output is correct |
23 |
Correct |
23 ms |
4948 KB |
Output is correct |
24 |
Correct |
20 ms |
4944 KB |
Output is correct |
25 |
Correct |
20 ms |
4940 KB |
Output is correct |
26 |
Correct |
20 ms |
5032 KB |
Output is correct |
27 |
Correct |
20 ms |
5032 KB |
Output is correct |
28 |
Correct |
20 ms |
5036 KB |
Output is correct |
29 |
Correct |
25 ms |
4940 KB |
Output is correct |
30 |
Correct |
20 ms |
5032 KB |
Output is correct |
31 |
Correct |
4 ms |
5032 KB |
Output is correct |
32 |
Correct |
6 ms |
4948 KB |
Output is correct |
33 |
Correct |
12 ms |
4948 KB |
Output is correct |
34 |
Correct |
8 ms |
4948 KB |
Output is correct |
35 |
Correct |
15 ms |
5032 KB |
Output is correct |
36 |
Correct |
52 ms |
4948 KB |
Output is correct |
37 |
Correct |
49 ms |
5020 KB |
Output is correct |
38 |
Correct |
104 ms |
5032 KB |
Output is correct |
39 |
Correct |
157 ms |
4948 KB |
Output is correct |
40 |
Correct |
264 ms |
5028 KB |
Output is correct |
41 |
Correct |
391 ms |
5040 KB |
Output is correct |
42 |
Correct |
384 ms |
4948 KB |
Output is correct |
43 |
Correct |
383 ms |
5068 KB |
Output is correct |
44 |
Correct |
374 ms |
5032 KB |
Output is correct |
45 |
Correct |
379 ms |
5028 KB |
Output is correct |
46 |
Correct |
380 ms |
4948 KB |
Output is correct |
47 |
Correct |
379 ms |
4964 KB |
Output is correct |
48 |
Correct |
378 ms |
4948 KB |
Output is correct |
49 |
Correct |
388 ms |
5028 KB |
Output is correct |
50 |
Correct |
379 ms |
4956 KB |
Output is correct |
51 |
Correct |
383 ms |
5040 KB |
Output is correct |
52 |
Correct |
385 ms |
4948 KB |
Output is correct |
53 |
Correct |
373 ms |
5068 KB |
Output is correct |
54 |
Correct |
374 ms |
4948 KB |
Output is correct |
55 |
Correct |
386 ms |
5028 KB |
Output is correct |
56 |
Correct |
384 ms |
5028 KB |
Output is correct |
57 |
Correct |
402 ms |
5024 KB |
Output is correct |
58 |
Correct |
373 ms |
5028 KB |
Output is correct |
59 |
Correct |
383 ms |
5024 KB |
Output is correct |
60 |
Correct |
376 ms |
5028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
6 ms |
4980 KB |
Output is correct |
15 |
Correct |
13 ms |
5028 KB |
Output is correct |
16 |
Correct |
14 ms |
5032 KB |
Output is correct |
17 |
Correct |
12 ms |
5024 KB |
Output is correct |
18 |
Correct |
13 ms |
4948 KB |
Output is correct |
19 |
Correct |
14 ms |
4948 KB |
Output is correct |
20 |
Correct |
13 ms |
4948 KB |
Output is correct |
21 |
Correct |
13 ms |
4940 KB |
Output is correct |
22 |
Correct |
14 ms |
4948 KB |
Output is correct |
23 |
Correct |
15 ms |
4944 KB |
Output is correct |
24 |
Correct |
13 ms |
4948 KB |
Output is correct |
25 |
Correct |
14 ms |
4944 KB |
Output is correct |
26 |
Correct |
2 ms |
4948 KB |
Output is correct |
27 |
Correct |
2 ms |
4948 KB |
Output is correct |
28 |
Correct |
4 ms |
4948 KB |
Output is correct |
29 |
Correct |
8 ms |
5032 KB |
Output is correct |
30 |
Correct |
14 ms |
4948 KB |
Output is correct |
31 |
Correct |
20 ms |
5032 KB |
Output is correct |
32 |
Correct |
20 ms |
5028 KB |
Output is correct |
33 |
Correct |
23 ms |
4948 KB |
Output is correct |
34 |
Correct |
20 ms |
4944 KB |
Output is correct |
35 |
Correct |
20 ms |
4940 KB |
Output is correct |
36 |
Correct |
20 ms |
5032 KB |
Output is correct |
37 |
Correct |
20 ms |
5032 KB |
Output is correct |
38 |
Correct |
20 ms |
5036 KB |
Output is correct |
39 |
Correct |
25 ms |
4940 KB |
Output is correct |
40 |
Correct |
20 ms |
5032 KB |
Output is correct |
41 |
Correct |
4 ms |
5032 KB |
Output is correct |
42 |
Correct |
6 ms |
4948 KB |
Output is correct |
43 |
Correct |
12 ms |
4948 KB |
Output is correct |
44 |
Correct |
8 ms |
4948 KB |
Output is correct |
45 |
Correct |
15 ms |
5032 KB |
Output is correct |
46 |
Correct |
52 ms |
4948 KB |
Output is correct |
47 |
Correct |
49 ms |
5020 KB |
Output is correct |
48 |
Correct |
104 ms |
5032 KB |
Output is correct |
49 |
Correct |
157 ms |
4948 KB |
Output is correct |
50 |
Correct |
264 ms |
5028 KB |
Output is correct |
51 |
Correct |
391 ms |
5040 KB |
Output is correct |
52 |
Correct |
384 ms |
4948 KB |
Output is correct |
53 |
Correct |
383 ms |
5068 KB |
Output is correct |
54 |
Correct |
374 ms |
5032 KB |
Output is correct |
55 |
Correct |
379 ms |
5028 KB |
Output is correct |
56 |
Correct |
380 ms |
4948 KB |
Output is correct |
57 |
Correct |
379 ms |
4964 KB |
Output is correct |
58 |
Correct |
378 ms |
4948 KB |
Output is correct |
59 |
Correct |
388 ms |
5028 KB |
Output is correct |
60 |
Correct |
379 ms |
4956 KB |
Output is correct |
61 |
Correct |
383 ms |
5040 KB |
Output is correct |
62 |
Correct |
385 ms |
4948 KB |
Output is correct |
63 |
Correct |
373 ms |
5068 KB |
Output is correct |
64 |
Correct |
374 ms |
4948 KB |
Output is correct |
65 |
Correct |
386 ms |
5028 KB |
Output is correct |
66 |
Correct |
384 ms |
5028 KB |
Output is correct |
67 |
Correct |
402 ms |
5024 KB |
Output is correct |
68 |
Correct |
373 ms |
5028 KB |
Output is correct |
69 |
Correct |
383 ms |
5024 KB |
Output is correct |
70 |
Correct |
376 ms |
5028 KB |
Output is correct |
71 |
Correct |
191 ms |
5048 KB |
Output is correct |
72 |
Correct |
199 ms |
5264 KB |
Output is correct |
73 |
Correct |
187 ms |
5264 KB |
Output is correct |
74 |
Correct |
195 ms |
5260 KB |
Output is correct |
75 |
Correct |
198 ms |
5268 KB |
Output is correct |
76 |
Correct |
738 ms |
5564 KB |
Output is correct |
77 |
Correct |
753 ms |
5564 KB |
Output is correct |
78 |
Correct |
765 ms |
5568 KB |
Output is correct |
79 |
Correct |
769 ms |
5580 KB |
Output is correct |
80 |
Correct |
753 ms |
5572 KB |
Output is correct |
81 |
Correct |
2611 ms |
6120 KB |
Output is correct |
82 |
Correct |
2583 ms |
6152 KB |
Output is correct |
83 |
Correct |
2674 ms |
6128 KB |
Output is correct |
84 |
Correct |
2653 ms |
6128 KB |
Output is correct |
85 |
Correct |
2618 ms |
6128 KB |
Output is correct |
86 |
Correct |
5314 ms |
6388 KB |
Output is correct |
87 |
Correct |
5352 ms |
6388 KB |
Output is correct |
88 |
Correct |
5332 ms |
6396 KB |
Output is correct |
89 |
Correct |
5298 ms |
6384 KB |
Output is correct |
90 |
Correct |
5308 ms |
6412 KB |
Output is correct |
91 |
Execution timed out |
7097 ms |
8268 KB |
Time limit exceeded |
92 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
6 ms |
4980 KB |
Output is correct |
15 |
Correct |
13 ms |
5028 KB |
Output is correct |
16 |
Correct |
14 ms |
5032 KB |
Output is correct |
17 |
Correct |
12 ms |
5024 KB |
Output is correct |
18 |
Correct |
13 ms |
4948 KB |
Output is correct |
19 |
Correct |
14 ms |
4948 KB |
Output is correct |
20 |
Correct |
13 ms |
4948 KB |
Output is correct |
21 |
Correct |
13 ms |
4940 KB |
Output is correct |
22 |
Correct |
14 ms |
4948 KB |
Output is correct |
23 |
Correct |
15 ms |
4944 KB |
Output is correct |
24 |
Correct |
13 ms |
4948 KB |
Output is correct |
25 |
Correct |
14 ms |
4944 KB |
Output is correct |
26 |
Correct |
2 ms |
4948 KB |
Output is correct |
27 |
Correct |
2 ms |
4948 KB |
Output is correct |
28 |
Correct |
4 ms |
4948 KB |
Output is correct |
29 |
Correct |
8 ms |
5032 KB |
Output is correct |
30 |
Correct |
14 ms |
4948 KB |
Output is correct |
31 |
Correct |
20 ms |
5032 KB |
Output is correct |
32 |
Correct |
20 ms |
5028 KB |
Output is correct |
33 |
Correct |
23 ms |
4948 KB |
Output is correct |
34 |
Correct |
20 ms |
4944 KB |
Output is correct |
35 |
Correct |
20 ms |
4940 KB |
Output is correct |
36 |
Correct |
20 ms |
5032 KB |
Output is correct |
37 |
Correct |
20 ms |
5032 KB |
Output is correct |
38 |
Correct |
20 ms |
5036 KB |
Output is correct |
39 |
Correct |
25 ms |
4940 KB |
Output is correct |
40 |
Correct |
20 ms |
5032 KB |
Output is correct |
41 |
Correct |
4 ms |
5032 KB |
Output is correct |
42 |
Correct |
6 ms |
4948 KB |
Output is correct |
43 |
Correct |
12 ms |
4948 KB |
Output is correct |
44 |
Correct |
8 ms |
4948 KB |
Output is correct |
45 |
Correct |
15 ms |
5032 KB |
Output is correct |
46 |
Correct |
52 ms |
4948 KB |
Output is correct |
47 |
Correct |
49 ms |
5020 KB |
Output is correct |
48 |
Correct |
104 ms |
5032 KB |
Output is correct |
49 |
Correct |
157 ms |
4948 KB |
Output is correct |
50 |
Correct |
264 ms |
5028 KB |
Output is correct |
51 |
Correct |
391 ms |
5040 KB |
Output is correct |
52 |
Correct |
384 ms |
4948 KB |
Output is correct |
53 |
Correct |
383 ms |
5068 KB |
Output is correct |
54 |
Correct |
374 ms |
5032 KB |
Output is correct |
55 |
Correct |
379 ms |
5028 KB |
Output is correct |
56 |
Correct |
380 ms |
4948 KB |
Output is correct |
57 |
Correct |
379 ms |
4964 KB |
Output is correct |
58 |
Correct |
378 ms |
4948 KB |
Output is correct |
59 |
Correct |
388 ms |
5028 KB |
Output is correct |
60 |
Correct |
379 ms |
4956 KB |
Output is correct |
61 |
Correct |
383 ms |
5040 KB |
Output is correct |
62 |
Correct |
385 ms |
4948 KB |
Output is correct |
63 |
Correct |
373 ms |
5068 KB |
Output is correct |
64 |
Correct |
374 ms |
4948 KB |
Output is correct |
65 |
Correct |
386 ms |
5028 KB |
Output is correct |
66 |
Correct |
384 ms |
5028 KB |
Output is correct |
67 |
Correct |
402 ms |
5024 KB |
Output is correct |
68 |
Correct |
373 ms |
5028 KB |
Output is correct |
69 |
Correct |
383 ms |
5024 KB |
Output is correct |
70 |
Correct |
376 ms |
5028 KB |
Output is correct |
71 |
Correct |
191 ms |
5048 KB |
Output is correct |
72 |
Correct |
199 ms |
5264 KB |
Output is correct |
73 |
Correct |
187 ms |
5264 KB |
Output is correct |
74 |
Correct |
195 ms |
5260 KB |
Output is correct |
75 |
Correct |
198 ms |
5268 KB |
Output is correct |
76 |
Correct |
738 ms |
5564 KB |
Output is correct |
77 |
Correct |
753 ms |
5564 KB |
Output is correct |
78 |
Correct |
765 ms |
5568 KB |
Output is correct |
79 |
Correct |
769 ms |
5580 KB |
Output is correct |
80 |
Correct |
753 ms |
5572 KB |
Output is correct |
81 |
Correct |
2611 ms |
6120 KB |
Output is correct |
82 |
Correct |
2583 ms |
6152 KB |
Output is correct |
83 |
Correct |
2674 ms |
6128 KB |
Output is correct |
84 |
Correct |
2653 ms |
6128 KB |
Output is correct |
85 |
Correct |
2618 ms |
6128 KB |
Output is correct |
86 |
Correct |
5314 ms |
6388 KB |
Output is correct |
87 |
Correct |
5352 ms |
6388 KB |
Output is correct |
88 |
Correct |
5332 ms |
6396 KB |
Output is correct |
89 |
Correct |
5298 ms |
6384 KB |
Output is correct |
90 |
Correct |
5308 ms |
6412 KB |
Output is correct |
91 |
Execution timed out |
7097 ms |
8268 KB |
Time limit exceeded |
92 |
Halted |
0 ms |
0 KB |
- |