#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int c[5005]; int a[5005]; int ans,n; int lowbit(int x) { return x&(-x); } int Sum(int end) { int sum=0; while(end>0) { sum+=c[end]; end=end-lowbit(end); } return sum; } void change(int i,int x) { while(i<=n) { c[i]=c[i]+x; i=i+lowbit(i); } } int main() { while(scanf("%d",&n)!=EOF) { int tmp=0; int s; for(int i=1;i<=n;i++) { c[i]=0; scanf("%d",&s); a[i]=s+1; } for(int i=1;i<=n;i++) { change(a[i],1); tmp+=(i-Sum(a[i])); } ans=tmp; int now; for(int i=1;i<n;i++) { now=tmp+(n+1-2*a[i]); tmp=now; ans=min(ans,now); } printf("%d\n",ans); } return 0; }