Input
Output
Sample Input
Sample Output
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<cmath>
using namespace std;
int a[1010];
int dp[1010];
int main()
{
int n;
while(~scanf("%d", &n)){
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n ; i++)
scanf("%d", &a[i]);
for(int i = 1; i <= n ; i++){
int max1 = a[i];
int min1 = a[i];
for(int j = i - 1; j >= 1 ; j--){
max1 = max(max1, a[j]);
min1 = min(min1, a[j]);
dp[i] = max(dp[i], dp[j-1] + (max1 - min1) * (max1 - min1));
}
}
printf("%d\n", dp[n]);
}
return 0;
}
原文:http://www.cnblogs.com/zero-begin/p/4649578.html