#include <bits/stdc++.h>
#define se second
#define fs first
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 5e3 + 7;
const ll Mod = 1e9 + 7;
const int szBL = 916;
const ll INF = 1e9;
const int BASE = 137;
int n, Q;
int a[N];
int pos[N];
int cnt[N][N], cost[N][N];
int dp[N];
void solution () {
cin >> n;
rep (i, 1, n) {
cin >> a[i];
pos[a[i]] = i;
}
rep (i, 1, n) {
rep (j, 1, n) {
cnt[i][j] = cnt[i - 1][j] + (a[i] >= j);
}
}
rep (i, 1, n)
rep (j, i, n) {
cost[i][j] = cost[i][j - 1] + cnt[pos[j]][i] - cnt[pos[j]][j];
}
dp[0] = 0;
rep (i, 1, n) {
dp[i] = 1e9;
int curcost = 0;
reb (j, i, 1) {
curcost += cnt[pos[j]][i + 1];
dp[i]= min(dp[i], dp[j - 1] + curcost + cost[j][i]);
}
}
cout << dp[n] <<"\n";
}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
2140 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
1 ms |
2264 KB |
Output is correct |
24 |
Correct |
1 ms |
2140 KB |
Output is correct |
25 |
Correct |
1 ms |
2136 KB |
Output is correct |
26 |
Correct |
1 ms |
2140 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
1 ms |
2140 KB |
Output is correct |
29 |
Correct |
1 ms |
2140 KB |
Output is correct |
30 |
Correct |
1 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
2140 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
1 ms |
2264 KB |
Output is correct |
24 |
Correct |
1 ms |
2140 KB |
Output is correct |
25 |
Correct |
1 ms |
2136 KB |
Output is correct |
26 |
Correct |
1 ms |
2140 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
1 ms |
2140 KB |
Output is correct |
29 |
Correct |
1 ms |
2140 KB |
Output is correct |
30 |
Correct |
1 ms |
2140 KB |
Output is correct |
31 |
Correct |
6 ms |
10584 KB |
Output is correct |
32 |
Correct |
8 ms |
10584 KB |
Output is correct |
33 |
Correct |
7 ms |
10588 KB |
Output is correct |
34 |
Correct |
6 ms |
10588 KB |
Output is correct |
35 |
Correct |
6 ms |
10588 KB |
Output is correct |
36 |
Correct |
8 ms |
10588 KB |
Output is correct |
37 |
Correct |
6 ms |
10668 KB |
Output is correct |
38 |
Correct |
7 ms |
10588 KB |
Output is correct |
39 |
Correct |
6 ms |
10584 KB |
Output is correct |
40 |
Correct |
7 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
2140 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
1 ms |
2264 KB |
Output is correct |
24 |
Correct |
1 ms |
2140 KB |
Output is correct |
25 |
Correct |
1 ms |
2136 KB |
Output is correct |
26 |
Correct |
1 ms |
2140 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
1 ms |
2140 KB |
Output is correct |
29 |
Correct |
1 ms |
2140 KB |
Output is correct |
30 |
Correct |
1 ms |
2140 KB |
Output is correct |
31 |
Correct |
6 ms |
10584 KB |
Output is correct |
32 |
Correct |
8 ms |
10584 KB |
Output is correct |
33 |
Correct |
7 ms |
10588 KB |
Output is correct |
34 |
Correct |
6 ms |
10588 KB |
Output is correct |
35 |
Correct |
6 ms |
10588 KB |
Output is correct |
36 |
Correct |
8 ms |
10588 KB |
Output is correct |
37 |
Correct |
6 ms |
10668 KB |
Output is correct |
38 |
Correct |
7 ms |
10588 KB |
Output is correct |
39 |
Correct |
6 ms |
10584 KB |
Output is correct |
40 |
Correct |
7 ms |
10588 KB |
Output is correct |
41 |
Correct |
291 ms |
165176 KB |
Output is correct |
42 |
Correct |
286 ms |
165200 KB |
Output is correct |
43 |
Correct |
289 ms |
165220 KB |
Output is correct |
44 |
Correct |
295 ms |
165204 KB |
Output is correct |
45 |
Correct |
305 ms |
165204 KB |
Output is correct |
46 |
Correct |
312 ms |
165200 KB |
Output is correct |
47 |
Correct |
325 ms |
165204 KB |
Output is correct |
48 |
Correct |
317 ms |
165204 KB |
Output is correct |
49 |
Correct |
298 ms |
165196 KB |
Output is correct |
50 |
Correct |
324 ms |
165352 KB |
Output is correct |
51 |
Correct |
288 ms |
165204 KB |
Output is correct |
52 |
Correct |
314 ms |
165228 KB |
Output is correct |
53 |
Correct |
300 ms |
165372 KB |
Output is correct |
54 |
Correct |
307 ms |
165204 KB |
Output is correct |
55 |
Correct |
291 ms |
165208 KB |
Output is correct |