#include <iostream>
#define ll long long
using namespace std;
const int N = 5e3+5;
ll n, a[N], id[N], dp[N], cnt[N][N], cost[N][N];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
id[a[i]] = i;
}
for(int i = 1; i <= n; ++i) {
for(int j = n; j >= 1; --j) {
cnt[i][j] = cnt[i][j+1] + (id[j] < id[i]);
}
}
for(int L = 1; L <= n; ++L) {
for(int R = L; R <= n; ++R) {
cost[L][R] = cost[L][R - 1] + cnt[R][L] - (R - L - (cnt[R][L] - cnt[R][R+1]));
}
}
for(int i = 1; i <= n; ++i) {
dp[i] = 1e15;
for(int j = 0; j < i; ++j) {
dp[i] = min(dp[i], dp[j] + cost[j+1][i]);
}
}
cout << dp[n] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
3 ms |
2412 KB |
Output is correct |
22 |
Correct |
3 ms |
2412 KB |
Output is correct |
23 |
Correct |
3 ms |
2412 KB |
Output is correct |
24 |
Correct |
3 ms |
2412 KB |
Output is correct |
25 |
Correct |
3 ms |
2412 KB |
Output is correct |
26 |
Correct |
3 ms |
2436 KB |
Output is correct |
27 |
Correct |
3 ms |
2412 KB |
Output is correct |
28 |
Correct |
3 ms |
2412 KB |
Output is correct |
29 |
Correct |
4 ms |
2412 KB |
Output is correct |
30 |
Correct |
3 ms |
2412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
3 ms |
2412 KB |
Output is correct |
22 |
Correct |
3 ms |
2412 KB |
Output is correct |
23 |
Correct |
3 ms |
2412 KB |
Output is correct |
24 |
Correct |
3 ms |
2412 KB |
Output is correct |
25 |
Correct |
3 ms |
2412 KB |
Output is correct |
26 |
Correct |
3 ms |
2436 KB |
Output is correct |
27 |
Correct |
3 ms |
2412 KB |
Output is correct |
28 |
Correct |
3 ms |
2412 KB |
Output is correct |
29 |
Correct |
4 ms |
2412 KB |
Output is correct |
30 |
Correct |
3 ms |
2412 KB |
Output is correct |
31 |
Correct |
17 ms |
14336 KB |
Output is correct |
32 |
Correct |
15 ms |
14316 KB |
Output is correct |
33 |
Correct |
15 ms |
14316 KB |
Output is correct |
34 |
Correct |
15 ms |
14444 KB |
Output is correct |
35 |
Correct |
15 ms |
14444 KB |
Output is correct |
36 |
Correct |
15 ms |
14444 KB |
Output is correct |
37 |
Correct |
15 ms |
14444 KB |
Output is correct |
38 |
Correct |
15 ms |
14444 KB |
Output is correct |
39 |
Correct |
15 ms |
14444 KB |
Output is correct |
40 |
Correct |
17 ms |
14444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
3 ms |
2412 KB |
Output is correct |
22 |
Correct |
3 ms |
2412 KB |
Output is correct |
23 |
Correct |
3 ms |
2412 KB |
Output is correct |
24 |
Correct |
3 ms |
2412 KB |
Output is correct |
25 |
Correct |
3 ms |
2412 KB |
Output is correct |
26 |
Correct |
3 ms |
2436 KB |
Output is correct |
27 |
Correct |
3 ms |
2412 KB |
Output is correct |
28 |
Correct |
3 ms |
2412 KB |
Output is correct |
29 |
Correct |
4 ms |
2412 KB |
Output is correct |
30 |
Correct |
3 ms |
2412 KB |
Output is correct |
31 |
Correct |
17 ms |
14336 KB |
Output is correct |
32 |
Correct |
15 ms |
14316 KB |
Output is correct |
33 |
Correct |
15 ms |
14316 KB |
Output is correct |
34 |
Correct |
15 ms |
14444 KB |
Output is correct |
35 |
Correct |
15 ms |
14444 KB |
Output is correct |
36 |
Correct |
15 ms |
14444 KB |
Output is correct |
37 |
Correct |
15 ms |
14444 KB |
Output is correct |
38 |
Correct |
15 ms |
14444 KB |
Output is correct |
39 |
Correct |
15 ms |
14444 KB |
Output is correct |
40 |
Correct |
17 ms |
14444 KB |
Output is correct |
41 |
Correct |
703 ms |
313196 KB |
Output is correct |
42 |
Correct |
710 ms |
313324 KB |
Output is correct |
43 |
Correct |
701 ms |
313324 KB |
Output is correct |
44 |
Correct |
698 ms |
313324 KB |
Output is correct |
45 |
Correct |
702 ms |
313248 KB |
Output is correct |
46 |
Correct |
760 ms |
313452 KB |
Output is correct |
47 |
Correct |
704 ms |
313452 KB |
Output is correct |
48 |
Correct |
718 ms |
313496 KB |
Output is correct |
49 |
Correct |
697 ms |
313452 KB |
Output is correct |
50 |
Correct |
732 ms |
313580 KB |
Output is correct |
51 |
Correct |
719 ms |
313452 KB |
Output is correct |
52 |
Correct |
696 ms |
313452 KB |
Output is correct |
53 |
Correct |
705 ms |
313324 KB |
Output is correct |
54 |
Correct |
742 ms |
313800 KB |
Output is correct |
55 |
Correct |
696 ms |
313452 KB |
Output is correct |