#include<bits/stdc++.h>
using namespace std;
#define int long long
#define nl '\n'
int pre1[5001][5001];
int pre2[5001][5001];
inline void solve(){
int n;
cin>>n;
int a[n+1];
for(int i=1; i<=n; i++) cin>>a[i];
for(int i=1; i<=n; i++){
for(int j=i+1; j<=n; j++){
pre1[a[j]][a[i]]++;
pre2[a[j]][a[i]]++;
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
pre1[i][j] += pre1[i][j-1];
}
}
for(int j=1; j<=n; j++){
for(int i=1; i<=n; i++){
pre2[i][j] += pre2[i-1][j];
}
}
int dp[n+1];
dp[0] = 0;
for(int i=1; i<=n; i++){
dp[i] = 1e18;
int add = 0;
for(int j=i; j>0; j--){
add += pre1[j][n] - pre1[j][i];
add += pre2[i][j] - pre2[j][j];
dp[i] = min(dp[i], dp[j-1] + add);
}
}
cout<<dp[n]<<nl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
int t = 1;
//cin>>t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4608 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4608 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
2 ms |
4432 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4608 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
2 ms |
4432 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
3 ms |
18768 KB |
Output is correct |
22 |
Correct |
3 ms |
18768 KB |
Output is correct |
23 |
Correct |
3 ms |
18768 KB |
Output is correct |
24 |
Correct |
3 ms |
18768 KB |
Output is correct |
25 |
Correct |
3 ms |
18936 KB |
Output is correct |
26 |
Correct |
3 ms |
18936 KB |
Output is correct |
27 |
Correct |
3 ms |
18768 KB |
Output is correct |
28 |
Correct |
3 ms |
18768 KB |
Output is correct |
29 |
Correct |
3 ms |
18768 KB |
Output is correct |
30 |
Correct |
3 ms |
18768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4608 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
2 ms |
4432 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
3 ms |
18768 KB |
Output is correct |
22 |
Correct |
3 ms |
18768 KB |
Output is correct |
23 |
Correct |
3 ms |
18768 KB |
Output is correct |
24 |
Correct |
3 ms |
18768 KB |
Output is correct |
25 |
Correct |
3 ms |
18936 KB |
Output is correct |
26 |
Correct |
3 ms |
18936 KB |
Output is correct |
27 |
Correct |
3 ms |
18768 KB |
Output is correct |
28 |
Correct |
3 ms |
18768 KB |
Output is correct |
29 |
Correct |
3 ms |
18768 KB |
Output is correct |
30 |
Correct |
3 ms |
18768 KB |
Output is correct |
31 |
Correct |
10 ms |
66128 KB |
Output is correct |
32 |
Correct |
9 ms |
66128 KB |
Output is correct |
33 |
Correct |
10 ms |
66128 KB |
Output is correct |
34 |
Correct |
11 ms |
66128 KB |
Output is correct |
35 |
Correct |
10 ms |
66128 KB |
Output is correct |
36 |
Correct |
10 ms |
66128 KB |
Output is correct |
37 |
Correct |
10 ms |
66104 KB |
Output is correct |
38 |
Correct |
11 ms |
66128 KB |
Output is correct |
39 |
Correct |
10 ms |
66128 KB |
Output is correct |
40 |
Correct |
10 ms |
66044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4608 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
2 ms |
4432 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
3 ms |
18768 KB |
Output is correct |
22 |
Correct |
3 ms |
18768 KB |
Output is correct |
23 |
Correct |
3 ms |
18768 KB |
Output is correct |
24 |
Correct |
3 ms |
18768 KB |
Output is correct |
25 |
Correct |
3 ms |
18936 KB |
Output is correct |
26 |
Correct |
3 ms |
18936 KB |
Output is correct |
27 |
Correct |
3 ms |
18768 KB |
Output is correct |
28 |
Correct |
3 ms |
18768 KB |
Output is correct |
29 |
Correct |
3 ms |
18768 KB |
Output is correct |
30 |
Correct |
3 ms |
18768 KB |
Output is correct |
31 |
Correct |
10 ms |
66128 KB |
Output is correct |
32 |
Correct |
9 ms |
66128 KB |
Output is correct |
33 |
Correct |
10 ms |
66128 KB |
Output is correct |
34 |
Correct |
11 ms |
66128 KB |
Output is correct |
35 |
Correct |
10 ms |
66128 KB |
Output is correct |
36 |
Correct |
10 ms |
66128 KB |
Output is correct |
37 |
Correct |
10 ms |
66104 KB |
Output is correct |
38 |
Correct |
11 ms |
66128 KB |
Output is correct |
39 |
Correct |
10 ms |
66128 KB |
Output is correct |
40 |
Correct |
10 ms |
66044 KB |
Output is correct |
41 |
Correct |
566 ms |
391752 KB |
Output is correct |
42 |
Correct |
647 ms |
391752 KB |
Output is correct |
43 |
Correct |
594 ms |
392016 KB |
Output is correct |
44 |
Correct |
648 ms |
391952 KB |
Output is correct |
45 |
Correct |
654 ms |
392004 KB |
Output is correct |
46 |
Correct |
607 ms |
392008 KB |
Output is correct |
47 |
Correct |
650 ms |
392008 KB |
Output is correct |
48 |
Correct |
684 ms |
392000 KB |
Output is correct |
49 |
Correct |
585 ms |
391964 KB |
Output is correct |
50 |
Correct |
631 ms |
392008 KB |
Output is correct |
51 |
Correct |
638 ms |
391896 KB |
Output is correct |
52 |
Correct |
631 ms |
392008 KB |
Output is correct |
53 |
Correct |
663 ms |
391984 KB |
Output is correct |
54 |
Correct |
664 ms |
391984 KB |
Output is correct |
55 |
Correct |
692 ms |
391848 KB |
Output is correct |