#include using namespace std; const int INF = 1000000000; int main(){ int N; cin >> N; vector P(N); for (int i = 0; i < N; i++){ cin >> P[i]; P[i]--; } vector> S(N + 1, vector(N + 1, 0)); for (int i = 0; i < N; i++){ for (int j = i + 1; j < N; j++){ if (P[i] > P[j]){ S[i][j + 1]++; } } } for (int i = 0; i <= N; i++){ for (int j = 0; j < N; j++){ S[i][j + 1] += S[i][j]; } } for (int i = N; i >= 1; i--){ for (int j = 0; j <= N; j++){ S[i - 1][j] += S[i][j]; } } vector dp(N + 1, -INF); dp[0] = 0; for (int i = 0; i < N; i++){ vector dp2(N + 1, -INF); for (int j = i; j < N; j++){ for (int k = j + 1; k <= N; k++){ dp2[k] = max(dp2[k], dp[j] + S[j][k]); } } cout << S[0][N] - dp2[N] << endl; swap(dp, dp2); } }