Maximum cost of 51nod 1270 arrays

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.