#include <bits/stdc++.h>
using namespace std;
const int MX=5005;
int N;
int H[MX], dp[MX], cnt[MX][MX], curCnt[MX], pos[MX], sum[MX][MX], diag[MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N;
for(int i=1;i<=N;i++) cin>>H[i];
for(int i=1;i<=N;i++) dp[i]=1e9;
for(int i=1;i<=N;i++) {
for(int j=H[i];j<=N;j++) curCnt[j]++;
for(int j=1;j<=N;j++) cnt[H[i]][j]=curCnt[j];
}
for(int j=1;j<=N;j++) {
for(int i=1;i<=N;i++) {
sum[i][j]+=sum[i-1][j];
sum[i][j]+=cnt[i][j];
}
}
for(int j=1;j<=N;j++) {
diag[j]+=diag[j-1];
diag[j]+=cnt[j][j-1];
}
for(int i=0;i<=N;i++) {
for(int k=i+1;k<=N;k++) {
int cost=0;
cost+=sum[k][N]-sum[i][N];
cost-=sum[k][k]-sum[i][k];
cost-=sum[k][i]-sum[i][i];
cost+=diag[k]-diag[i];
dp[k]=min(dp[k],dp[i]+cost);
}
}
cout<<dp[N]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2552 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2552 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 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 |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2552 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 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 |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
7512 KB |
Output is correct |
22 |
Correct |
2 ms |
7516 KB |
Output is correct |
23 |
Correct |
2 ms |
7516 KB |
Output is correct |
24 |
Correct |
2 ms |
7516 KB |
Output is correct |
25 |
Correct |
3 ms |
7516 KB |
Output is correct |
26 |
Correct |
2 ms |
7516 KB |
Output is correct |
27 |
Correct |
2 ms |
7512 KB |
Output is correct |
28 |
Correct |
2 ms |
7516 KB |
Output is correct |
29 |
Correct |
2 ms |
7516 KB |
Output is correct |
30 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2552 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 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 |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
7512 KB |
Output is correct |
22 |
Correct |
2 ms |
7516 KB |
Output is correct |
23 |
Correct |
2 ms |
7516 KB |
Output is correct |
24 |
Correct |
2 ms |
7516 KB |
Output is correct |
25 |
Correct |
3 ms |
7516 KB |
Output is correct |
26 |
Correct |
2 ms |
7516 KB |
Output is correct |
27 |
Correct |
2 ms |
7512 KB |
Output is correct |
28 |
Correct |
2 ms |
7516 KB |
Output is correct |
29 |
Correct |
2 ms |
7516 KB |
Output is correct |
30 |
Correct |
2 ms |
7516 KB |
Output is correct |
31 |
Correct |
16 ms |
24660 KB |
Output is correct |
32 |
Correct |
11 ms |
24668 KB |
Output is correct |
33 |
Correct |
11 ms |
24668 KB |
Output is correct |
34 |
Correct |
11 ms |
24668 KB |
Output is correct |
35 |
Correct |
12 ms |
24668 KB |
Output is correct |
36 |
Correct |
12 ms |
24668 KB |
Output is correct |
37 |
Correct |
11 ms |
24672 KB |
Output is correct |
38 |
Correct |
10 ms |
24552 KB |
Output is correct |
39 |
Correct |
11 ms |
24668 KB |
Output is correct |
40 |
Correct |
11 ms |
24668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2552 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 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 |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
7512 KB |
Output is correct |
22 |
Correct |
2 ms |
7516 KB |
Output is correct |
23 |
Correct |
2 ms |
7516 KB |
Output is correct |
24 |
Correct |
2 ms |
7516 KB |
Output is correct |
25 |
Correct |
3 ms |
7516 KB |
Output is correct |
26 |
Correct |
2 ms |
7516 KB |
Output is correct |
27 |
Correct |
2 ms |
7512 KB |
Output is correct |
28 |
Correct |
2 ms |
7516 KB |
Output is correct |
29 |
Correct |
2 ms |
7516 KB |
Output is correct |
30 |
Correct |
2 ms |
7516 KB |
Output is correct |
31 |
Correct |
16 ms |
24660 KB |
Output is correct |
32 |
Correct |
11 ms |
24668 KB |
Output is correct |
33 |
Correct |
11 ms |
24668 KB |
Output is correct |
34 |
Correct |
11 ms |
24668 KB |
Output is correct |
35 |
Correct |
12 ms |
24668 KB |
Output is correct |
36 |
Correct |
12 ms |
24668 KB |
Output is correct |
37 |
Correct |
11 ms |
24672 KB |
Output is correct |
38 |
Correct |
10 ms |
24552 KB |
Output is correct |
39 |
Correct |
11 ms |
24668 KB |
Output is correct |
40 |
Correct |
11 ms |
24668 KB |
Output is correct |
41 |
Correct |
883 ms |
196276 KB |
Output is correct |
42 |
Correct |
918 ms |
196512 KB |
Output is correct |
43 |
Correct |
936 ms |
196384 KB |
Output is correct |
44 |
Correct |
889 ms |
196532 KB |
Output is correct |
45 |
Correct |
901 ms |
196584 KB |
Output is correct |
46 |
Correct |
855 ms |
196484 KB |
Output is correct |
47 |
Correct |
834 ms |
196376 KB |
Output is correct |
48 |
Correct |
855 ms |
196552 KB |
Output is correct |
49 |
Correct |
861 ms |
196348 KB |
Output is correct |
50 |
Correct |
873 ms |
196552 KB |
Output is correct |
51 |
Correct |
892 ms |
196692 KB |
Output is correct |
52 |
Correct |
903 ms |
196560 KB |
Output is correct |
53 |
Correct |
873 ms |
196732 KB |
Output is correct |
54 |
Correct |
873 ms |
196548 KB |
Output is correct |
55 |
Correct |
892 ms |
196552 KB |
Output is correct |