Array A consists of n elements A1, A2 ... An. Array B consists of n elements B1, B2 ... BN. And each element of the array, AI, satisfies 1 <= ai <= Bi. The cost of array A is defined as follows: (the formula represents the sum of the absolute values of the difference of all two adjacent elements) given to arrays B, calculating the possible maximum cost S. Input
Line 1th: 1 number N, indicating the length of the array (1 <= n <= 50000). Line 2-n+1:1 numbers per line, corresponding to array element bi (1 <= bi <= 10000).
Output
The maximum cost of output s.
Input example
510110110
Output example
36
Optimal for extreme points. DP[I][1] Indicates the first point, Dp[i][0] represents the first I take 1
1#include <bits/stdc++.h>2 using namespacestd;3 #definell Long Long4 Const intN = 5e4+Ten;5 intB[n], N, dp[n][2];6 intMain () {7CIN >>N;8 for(inti =1; I <= N; i + +) cin >>B[i];9 for(inti =2; I <= N; i + +) {Tendp[i][1] = max (dp[i-1][0]+b[i]-1, dp[i-1][1]+abs (b[i]-b[i-1])); Onedp[i][0] = max (dp[i-1][0], dp[i-1][1]+b[i-1]-1); A } -printf"%d\n", Max (dp[n][0],dp[n][1])); - return 0; the}
Maximum cost of 51nod 1270 arrays