#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline void fastio() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
}
ll n, arr[5050], p[5050], dp[5050], sum[5050][5050];
int main() {
fastio();
cin >> n;
for (ll i = 1; i <= n; i++) {
cin >> arr[i];
p[arr[i]] = i;
}
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
sum[i][j] += sum[i - 1][j] + (arr[i] <= j);
}
}
memset(dp, 0x3f, sizeof(dp));
*dp = 0;
for (ll i = 1; i <= n; i++) {
for (ll j = i, tmp = 0; j > 0; j--) {
tmp += (i - j) + (sum[p[j]][n] - sum[p[j]][i]) - (sum[p[j]][i] - sum[p[j]][j]);
dp[i] = min(dp[i], dp[j - 1] + tmp);
}
}
cout << dp[n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2540 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2540 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2556 KB |
Output is correct |
14 |
Correct |
1 ms |
2512 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2540 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2556 KB |
Output is correct |
14 |
Correct |
1 ms |
2512 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8536 KB |
Output is correct |
25 |
Correct |
2 ms |
8536 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
1 ms |
8536 KB |
Output is correct |
28 |
Correct |
1 ms |
8536 KB |
Output is correct |
29 |
Correct |
2 ms |
8580 KB |
Output is correct |
30 |
Correct |
1 ms |
8536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2540 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2556 KB |
Output is correct |
14 |
Correct |
1 ms |
2512 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8536 KB |
Output is correct |
25 |
Correct |
2 ms |
8536 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
1 ms |
8536 KB |
Output is correct |
28 |
Correct |
1 ms |
8536 KB |
Output is correct |
29 |
Correct |
2 ms |
8580 KB |
Output is correct |
30 |
Correct |
1 ms |
8536 KB |
Output is correct |
31 |
Correct |
8 ms |
33304 KB |
Output is correct |
32 |
Correct |
5 ms |
33368 KB |
Output is correct |
33 |
Correct |
7 ms |
33372 KB |
Output is correct |
34 |
Correct |
6 ms |
33372 KB |
Output is correct |
35 |
Correct |
5 ms |
33372 KB |
Output is correct |
36 |
Correct |
5 ms |
33340 KB |
Output is correct |
37 |
Correct |
5 ms |
33372 KB |
Output is correct |
38 |
Correct |
5 ms |
33372 KB |
Output is correct |
39 |
Correct |
5 ms |
33372 KB |
Output is correct |
40 |
Correct |
6 ms |
33372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2540 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2556 KB |
Output is correct |
14 |
Correct |
1 ms |
2512 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8536 KB |
Output is correct |
25 |
Correct |
2 ms |
8536 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
1 ms |
8536 KB |
Output is correct |
28 |
Correct |
1 ms |
8536 KB |
Output is correct |
29 |
Correct |
2 ms |
8580 KB |
Output is correct |
30 |
Correct |
1 ms |
8536 KB |
Output is correct |
31 |
Correct |
8 ms |
33304 KB |
Output is correct |
32 |
Correct |
5 ms |
33368 KB |
Output is correct |
33 |
Correct |
7 ms |
33372 KB |
Output is correct |
34 |
Correct |
6 ms |
33372 KB |
Output is correct |
35 |
Correct |
5 ms |
33372 KB |
Output is correct |
36 |
Correct |
5 ms |
33340 KB |
Output is correct |
37 |
Correct |
5 ms |
33372 KB |
Output is correct |
38 |
Correct |
5 ms |
33372 KB |
Output is correct |
39 |
Correct |
5 ms |
33372 KB |
Output is correct |
40 |
Correct |
6 ms |
33372 KB |
Output is correct |
41 |
Correct |
146 ms |
199764 KB |
Output is correct |
42 |
Correct |
125 ms |
199716 KB |
Output is correct |
43 |
Correct |
156 ms |
199764 KB |
Output is correct |
44 |
Correct |
147 ms |
199700 KB |
Output is correct |
45 |
Correct |
155 ms |
199764 KB |
Output is correct |
46 |
Correct |
152 ms |
199772 KB |
Output is correct |
47 |
Correct |
159 ms |
199768 KB |
Output is correct |
48 |
Correct |
134 ms |
199532 KB |
Output is correct |
49 |
Correct |
155 ms |
199768 KB |
Output is correct |
50 |
Correct |
158 ms |
199764 KB |
Output is correct |
51 |
Correct |
153 ms |
199776 KB |
Output is correct |
52 |
Correct |
157 ms |
199776 KB |
Output is correct |
53 |
Correct |
152 ms |
199772 KB |
Output is correct |
54 |
Correct |
128 ms |
199764 KB |
Output is correct |
55 |
Correct |
154 ms |
199772 KB |
Output is correct |