标签:
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<cmath> 6 #define INF 100000000000000 7 #define maxn 2005 8 using namespace std; 9 int n; 10 typedef long long LL; 11 LL a[maxn],b[maxn]; 12 LL dp[maxn][maxn]; 13 int main() 14 { 15 while(scanf("%d",&n)!=EOF) 16 { 17 for(int i=1; i<=n; i++) 18 { 19 scanf("%I64d",&a[i]); 20 b[i]=a[i]; 21 } 22 sort(b+1,b+1+n); 23 for(int i=1; i<=n; i++) 24 for(int j=1; j<=n; j++) 25 dp[i][j]=INF; 26 for(int j=1; j<=n; j++) 27 { 28 dp[1][j]=(a[1]-b[j]); 29 if(dp[1][j]<0) 30 dp[1][j]=-dp[1][j]; 31 } 32 33 for(int i=2; i<=n; i++) 34 { 35 LL k=dp[i-1][1]; 36 for(int j=1; j<=n; j++) 37 { 38 k=min(dp[i-1][j],k); 39 LL tem=(a[i]-b[j]); 40 if(tem<0) 41 tem=-tem; 42 dp[i][j]=min(dp[i][j],k+tem); 43 // printf("==%I64d\n",dp[i][j]); 44 } 45 } 46 LL minn=INF; 47 for(int i=1; i<=n; i++) 48 minn=min(minn,dp[n][i]); 49 printf("%I64d\n",minn); 50 } 51 return 0; 52 }
标签:
原文地址:http://www.cnblogs.com/tsw123/p/4415737.html