#include "bits/stdc++.h" using namespace std; const int N=2e3+3; int n,a[N],b[N],f[N][N]; int A=1e9; void sov(){ int i,j,k; for(i=1;i<=n;i++)b[i]=a[i]; sort(b+1,b+1+n); for(i=1;i<=n;i++){ k=1e9; for(j=1;j<=n;j++){ k=min(k,f[i-1][j]); f[i][j]=k+abs(a[i]-b[j]); } } int ans=1e9; for(i=1;i<=n;i++) ans=min(ans,f[n][i]); A=min(A,ans); } main(){ cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; sov(); reverse(a+1,a+1+n); sov(); cout<<A; }
标签:std,int,sov,273,分级,acwing From: https://www.cnblogs.com/towboa/p/16945998.html