#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
using i64 = std::int64_t;
constexpr i64 INF = std::numeric_limits<i64>::max() / 2;
template <class T> void setmin(T& lhs, const T& rhs) {
if (lhs > rhs) {
lhs = rhs;
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, Q;
std::cin >> N >> Q;
vector<int> A(N);
std::map<int, int> count;
for (auto& x : A) {
std::cin >> x;
count[x] += 1;
}
vector<int> block;
for (const auto& [_, c] : count) {
block.push_back(c);
}
std::sort(block.begin(), block.end());
const int n = block.size();
vector<i64> dp(N + 1, INF), next(N + 1);
dp[0] = (i64)N * (N + 1) / 2;
int sum = 0;
for (int i = 0; i < n - 1; ++i) {
std::fill(next.begin(), next.end(), INF);
for (int l = 0; l <= sum; ++l) {
if (dp[l] == INF) {
continue;
}
const int nl = l + block[i];
const int nr = N - (sum - l) - block[i];
setmin(next[nl], dp[l] + (i64)nl * (N - nl));
setmin(next[l], dp[l] + (i64)nr * (N - nr));
}
std::swap(dp, next);
sum += block[i];
}
std::cout << *std::min_element(dp.begin(), dp.end()) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
7 ms |
2380 KB |
Output is correct |
5 |
Correct |
14 ms |
4572 KB |
Output is correct |
6 |
Correct |
23 ms |
6856 KB |
Output is correct |
7 |
Correct |
21 ms |
6732 KB |
Output is correct |
8 |
Correct |
22 ms |
6732 KB |
Output is correct |
9 |
Correct |
25 ms |
6724 KB |
Output is correct |
10 |
Correct |
21 ms |
6724 KB |
Output is correct |
11 |
Correct |
22 ms |
6724 KB |
Output is correct |
12 |
Correct |
23 ms |
6840 KB |
Output is correct |
13 |
Correct |
26 ms |
6708 KB |
Output is correct |
14 |
Correct |
23 ms |
6708 KB |
Output is correct |
15 |
Correct |
22 ms |
6720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
7 ms |
2380 KB |
Output is correct |
5 |
Correct |
14 ms |
4572 KB |
Output is correct |
6 |
Correct |
23 ms |
6856 KB |
Output is correct |
7 |
Correct |
21 ms |
6732 KB |
Output is correct |
8 |
Correct |
22 ms |
6732 KB |
Output is correct |
9 |
Correct |
25 ms |
6724 KB |
Output is correct |
10 |
Correct |
21 ms |
6724 KB |
Output is correct |
11 |
Correct |
22 ms |
6724 KB |
Output is correct |
12 |
Correct |
23 ms |
6840 KB |
Output is correct |
13 |
Correct |
26 ms |
6708 KB |
Output is correct |
14 |
Correct |
23 ms |
6708 KB |
Output is correct |
15 |
Correct |
22 ms |
6720 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
12 ms |
2468 KB |
Output is correct |
20 |
Correct |
20 ms |
4684 KB |
Output is correct |
21 |
Correct |
29 ms |
6980 KB |
Output is correct |
22 |
Correct |
29 ms |
6864 KB |
Output is correct |
23 |
Correct |
29 ms |
6984 KB |
Output is correct |
24 |
Correct |
31 ms |
6852 KB |
Output is correct |
25 |
Correct |
29 ms |
6844 KB |
Output is correct |
26 |
Correct |
30 ms |
6856 KB |
Output is correct |
27 |
Correct |
29 ms |
6852 KB |
Output is correct |
28 |
Correct |
29 ms |
6820 KB |
Output is correct |
29 |
Correct |
30 ms |
6844 KB |
Output is correct |
30 |
Correct |
30 ms |
6888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
7 ms |
2380 KB |
Output is correct |
5 |
Correct |
14 ms |
4572 KB |
Output is correct |
6 |
Correct |
23 ms |
6856 KB |
Output is correct |
7 |
Correct |
21 ms |
6732 KB |
Output is correct |
8 |
Correct |
22 ms |
6732 KB |
Output is correct |
9 |
Correct |
25 ms |
6724 KB |
Output is correct |
10 |
Correct |
21 ms |
6724 KB |
Output is correct |
11 |
Correct |
22 ms |
6724 KB |
Output is correct |
12 |
Correct |
23 ms |
6840 KB |
Output is correct |
13 |
Correct |
26 ms |
6708 KB |
Output is correct |
14 |
Correct |
23 ms |
6708 KB |
Output is correct |
15 |
Correct |
22 ms |
6720 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
12 ms |
2468 KB |
Output is correct |
20 |
Correct |
20 ms |
4684 KB |
Output is correct |
21 |
Correct |
29 ms |
6980 KB |
Output is correct |
22 |
Correct |
29 ms |
6864 KB |
Output is correct |
23 |
Correct |
29 ms |
6984 KB |
Output is correct |
24 |
Correct |
31 ms |
6852 KB |
Output is correct |
25 |
Correct |
29 ms |
6844 KB |
Output is correct |
26 |
Correct |
30 ms |
6856 KB |
Output is correct |
27 |
Correct |
29 ms |
6852 KB |
Output is correct |
28 |
Correct |
29 ms |
6820 KB |
Output is correct |
29 |
Correct |
30 ms |
6844 KB |
Output is correct |
30 |
Correct |
30 ms |
6888 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
260 KB |
Output is correct |
34 |
Correct |
3 ms |
448 KB |
Output is correct |
35 |
Correct |
4 ms |
536 KB |
Output is correct |
36 |
Correct |
11 ms |
580 KB |
Output is correct |
37 |
Correct |
27 ms |
1500 KB |
Output is correct |
38 |
Correct |
57 ms |
1540 KB |
Output is correct |
39 |
Correct |
102 ms |
2720 KB |
Output is correct |
40 |
Correct |
263 ms |
4976 KB |
Output is correct |
41 |
Correct |
305 ms |
7372 KB |
Output is correct |
42 |
Correct |
304 ms |
7384 KB |
Output is correct |
43 |
Correct |
285 ms |
7364 KB |
Output is correct |
44 |
Correct |
322 ms |
7400 KB |
Output is correct |
45 |
Correct |
298 ms |
7376 KB |
Output is correct |
46 |
Correct |
308 ms |
7376 KB |
Output is correct |
47 |
Correct |
300 ms |
7384 KB |
Output is correct |
48 |
Correct |
301 ms |
7400 KB |
Output is correct |
49 |
Correct |
329 ms |
7268 KB |
Output is correct |
50 |
Correct |
322 ms |
7364 KB |
Output is correct |
51 |
Correct |
291 ms |
7256 KB |
Output is correct |
52 |
Correct |
309 ms |
7372 KB |
Output is correct |
53 |
Correct |
292 ms |
7268 KB |
Output is correct |
54 |
Correct |
287 ms |
7268 KB |
Output is correct |
55 |
Correct |
293 ms |
7272 KB |
Output is correct |
56 |
Correct |
293 ms |
7272 KB |
Output is correct |
57 |
Correct |
308 ms |
7380 KB |
Output is correct |
58 |
Correct |
288 ms |
7264 KB |
Output is correct |
59 |
Correct |
287 ms |
7264 KB |
Output is correct |
60 |
Correct |
291 ms |
7284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
7 ms |
2380 KB |
Output is correct |
15 |
Correct |
14 ms |
4572 KB |
Output is correct |
16 |
Correct |
23 ms |
6856 KB |
Output is correct |
17 |
Correct |
21 ms |
6732 KB |
Output is correct |
18 |
Correct |
22 ms |
6732 KB |
Output is correct |
19 |
Correct |
25 ms |
6724 KB |
Output is correct |
20 |
Correct |
21 ms |
6724 KB |
Output is correct |
21 |
Correct |
22 ms |
6724 KB |
Output is correct |
22 |
Correct |
23 ms |
6840 KB |
Output is correct |
23 |
Correct |
26 ms |
6708 KB |
Output is correct |
24 |
Correct |
23 ms |
6708 KB |
Output is correct |
25 |
Correct |
22 ms |
6720 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
308 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
12 ms |
2468 KB |
Output is correct |
30 |
Correct |
20 ms |
4684 KB |
Output is correct |
31 |
Correct |
29 ms |
6980 KB |
Output is correct |
32 |
Correct |
29 ms |
6864 KB |
Output is correct |
33 |
Correct |
29 ms |
6984 KB |
Output is correct |
34 |
Correct |
31 ms |
6852 KB |
Output is correct |
35 |
Correct |
29 ms |
6844 KB |
Output is correct |
36 |
Correct |
30 ms |
6856 KB |
Output is correct |
37 |
Correct |
29 ms |
6852 KB |
Output is correct |
38 |
Correct |
29 ms |
6820 KB |
Output is correct |
39 |
Correct |
30 ms |
6844 KB |
Output is correct |
40 |
Correct |
30 ms |
6888 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
260 KB |
Output is correct |
44 |
Correct |
3 ms |
448 KB |
Output is correct |
45 |
Correct |
4 ms |
536 KB |
Output is correct |
46 |
Correct |
11 ms |
580 KB |
Output is correct |
47 |
Correct |
27 ms |
1500 KB |
Output is correct |
48 |
Correct |
57 ms |
1540 KB |
Output is correct |
49 |
Correct |
102 ms |
2720 KB |
Output is correct |
50 |
Correct |
263 ms |
4976 KB |
Output is correct |
51 |
Correct |
305 ms |
7372 KB |
Output is correct |
52 |
Correct |
304 ms |
7384 KB |
Output is correct |
53 |
Correct |
285 ms |
7364 KB |
Output is correct |
54 |
Correct |
322 ms |
7400 KB |
Output is correct |
55 |
Correct |
298 ms |
7376 KB |
Output is correct |
56 |
Correct |
308 ms |
7376 KB |
Output is correct |
57 |
Correct |
300 ms |
7384 KB |
Output is correct |
58 |
Correct |
301 ms |
7400 KB |
Output is correct |
59 |
Correct |
329 ms |
7268 KB |
Output is correct |
60 |
Correct |
322 ms |
7364 KB |
Output is correct |
61 |
Correct |
291 ms |
7256 KB |
Output is correct |
62 |
Correct |
309 ms |
7372 KB |
Output is correct |
63 |
Correct |
292 ms |
7268 KB |
Output is correct |
64 |
Correct |
287 ms |
7268 KB |
Output is correct |
65 |
Correct |
293 ms |
7272 KB |
Output is correct |
66 |
Correct |
293 ms |
7272 KB |
Output is correct |
67 |
Correct |
308 ms |
7380 KB |
Output is correct |
68 |
Correct |
288 ms |
7264 KB |
Output is correct |
69 |
Correct |
287 ms |
7264 KB |
Output is correct |
70 |
Correct |
291 ms |
7284 KB |
Output is correct |
71 |
Correct |
94 ms |
1576 KB |
Output is correct |
72 |
Correct |
106 ms |
1620 KB |
Output is correct |
73 |
Correct |
106 ms |
1620 KB |
Output is correct |
74 |
Correct |
95 ms |
1596 KB |
Output is correct |
75 |
Correct |
93 ms |
1612 KB |
Output is correct |
76 |
Correct |
451 ms |
3064 KB |
Output is correct |
77 |
Correct |
459 ms |
3032 KB |
Output is correct |
78 |
Correct |
464 ms |
3032 KB |
Output is correct |
79 |
Correct |
490 ms |
3032 KB |
Output is correct |
80 |
Correct |
471 ms |
3112 KB |
Output is correct |
81 |
Correct |
1813 ms |
5752 KB |
Output is correct |
82 |
Correct |
1777 ms |
5760 KB |
Output is correct |
83 |
Correct |
1782 ms |
5752 KB |
Output is correct |
84 |
Correct |
1787 ms |
5760 KB |
Output is correct |
85 |
Correct |
1747 ms |
5760 KB |
Output is correct |
86 |
Correct |
3656 ms |
6516 KB |
Output is correct |
87 |
Correct |
3599 ms |
6468 KB |
Output is correct |
88 |
Correct |
3566 ms |
6468 KB |
Output is correct |
89 |
Correct |
3546 ms |
6468 KB |
Output is correct |
90 |
Correct |
3617 ms |
6428 KB |
Output is correct |
91 |
Execution timed out |
7058 ms |
13048 KB |
Time limit exceeded |
92 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
7 ms |
2380 KB |
Output is correct |
15 |
Correct |
14 ms |
4572 KB |
Output is correct |
16 |
Correct |
23 ms |
6856 KB |
Output is correct |
17 |
Correct |
21 ms |
6732 KB |
Output is correct |
18 |
Correct |
22 ms |
6732 KB |
Output is correct |
19 |
Correct |
25 ms |
6724 KB |
Output is correct |
20 |
Correct |
21 ms |
6724 KB |
Output is correct |
21 |
Correct |
22 ms |
6724 KB |
Output is correct |
22 |
Correct |
23 ms |
6840 KB |
Output is correct |
23 |
Correct |
26 ms |
6708 KB |
Output is correct |
24 |
Correct |
23 ms |
6708 KB |
Output is correct |
25 |
Correct |
22 ms |
6720 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
308 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
12 ms |
2468 KB |
Output is correct |
30 |
Correct |
20 ms |
4684 KB |
Output is correct |
31 |
Correct |
29 ms |
6980 KB |
Output is correct |
32 |
Correct |
29 ms |
6864 KB |
Output is correct |
33 |
Correct |
29 ms |
6984 KB |
Output is correct |
34 |
Correct |
31 ms |
6852 KB |
Output is correct |
35 |
Correct |
29 ms |
6844 KB |
Output is correct |
36 |
Correct |
30 ms |
6856 KB |
Output is correct |
37 |
Correct |
29 ms |
6852 KB |
Output is correct |
38 |
Correct |
29 ms |
6820 KB |
Output is correct |
39 |
Correct |
30 ms |
6844 KB |
Output is correct |
40 |
Correct |
30 ms |
6888 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
260 KB |
Output is correct |
44 |
Correct |
3 ms |
448 KB |
Output is correct |
45 |
Correct |
4 ms |
536 KB |
Output is correct |
46 |
Correct |
11 ms |
580 KB |
Output is correct |
47 |
Correct |
27 ms |
1500 KB |
Output is correct |
48 |
Correct |
57 ms |
1540 KB |
Output is correct |
49 |
Correct |
102 ms |
2720 KB |
Output is correct |
50 |
Correct |
263 ms |
4976 KB |
Output is correct |
51 |
Correct |
305 ms |
7372 KB |
Output is correct |
52 |
Correct |
304 ms |
7384 KB |
Output is correct |
53 |
Correct |
285 ms |
7364 KB |
Output is correct |
54 |
Correct |
322 ms |
7400 KB |
Output is correct |
55 |
Correct |
298 ms |
7376 KB |
Output is correct |
56 |
Correct |
308 ms |
7376 KB |
Output is correct |
57 |
Correct |
300 ms |
7384 KB |
Output is correct |
58 |
Correct |
301 ms |
7400 KB |
Output is correct |
59 |
Correct |
329 ms |
7268 KB |
Output is correct |
60 |
Correct |
322 ms |
7364 KB |
Output is correct |
61 |
Correct |
291 ms |
7256 KB |
Output is correct |
62 |
Correct |
309 ms |
7372 KB |
Output is correct |
63 |
Correct |
292 ms |
7268 KB |
Output is correct |
64 |
Correct |
287 ms |
7268 KB |
Output is correct |
65 |
Correct |
293 ms |
7272 KB |
Output is correct |
66 |
Correct |
293 ms |
7272 KB |
Output is correct |
67 |
Correct |
308 ms |
7380 KB |
Output is correct |
68 |
Correct |
288 ms |
7264 KB |
Output is correct |
69 |
Correct |
287 ms |
7264 KB |
Output is correct |
70 |
Correct |
291 ms |
7284 KB |
Output is correct |
71 |
Correct |
94 ms |
1576 KB |
Output is correct |
72 |
Correct |
106 ms |
1620 KB |
Output is correct |
73 |
Correct |
106 ms |
1620 KB |
Output is correct |
74 |
Correct |
95 ms |
1596 KB |
Output is correct |
75 |
Correct |
93 ms |
1612 KB |
Output is correct |
76 |
Correct |
451 ms |
3064 KB |
Output is correct |
77 |
Correct |
459 ms |
3032 KB |
Output is correct |
78 |
Correct |
464 ms |
3032 KB |
Output is correct |
79 |
Correct |
490 ms |
3032 KB |
Output is correct |
80 |
Correct |
471 ms |
3112 KB |
Output is correct |
81 |
Correct |
1813 ms |
5752 KB |
Output is correct |
82 |
Correct |
1777 ms |
5760 KB |
Output is correct |
83 |
Correct |
1782 ms |
5752 KB |
Output is correct |
84 |
Correct |
1787 ms |
5760 KB |
Output is correct |
85 |
Correct |
1747 ms |
5760 KB |
Output is correct |
86 |
Correct |
3656 ms |
6516 KB |
Output is correct |
87 |
Correct |
3599 ms |
6468 KB |
Output is correct |
88 |
Correct |
3566 ms |
6468 KB |
Output is correct |
89 |
Correct |
3546 ms |
6468 KB |
Output is correct |
90 |
Correct |
3617 ms |
6428 KB |
Output is correct |
91 |
Execution timed out |
7058 ms |
13048 KB |
Time limit exceeded |
92 |
Halted |
0 ms |
0 KB |
- |