Codeforces Round #276 (Div. 1) D.kindergarten DP greedy

Source: Internet
Author: User

D. Kindergarten

In a kindergarten, the children is being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should is a nonempty segment of consecutive children of a line. A Group ' s sociability is the maximum difference of charisma of the Children in the group (in particular, if the group Consists of one child, its sociability equals a zero).

The teacher wants to divide the children into some number of groups in such by that the total sociability of the Grou PS is maximum. Help him find this value.

Input

The first line contains an integer n-the number of children in the line (1≤ n ≤106).

The second line contains n integers ai -the charisma of the i-th Child ( -9≤ ai ≤109).

Output

Print the maximum possible total sociability of all groups.

Sample Test (s) input
5
1 2 3) 1 2
Output
3
Note

In the first test sample one of the possible variants of a division is following:the first three children form a group W ITH sociability 2, and the remaining children form a group with sociability 1.

In the Second Test sample any division leads to the same result, the sociability would be equal to 0 in each group.

Test Instructions : give you n consecutive number, let you divide into continuous interval, each interval value for this interval the difference between the maximum minimum value, ask you this n number formation of the maximum value is how much

Puzzle: One idea of greed is that monotony must be in the same interval, knowing this is good.

Consider the V-shaped inverted V-shaped.

///1085422276#include<bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")using namespacestd; typedefLong Longll;typedef unsignedLong Longull;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_backinline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){        if(ch=='-') f=-1; ch=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar (); }returnx*F;}//****************************************Const intn=1000000+ -;#defineMoD 10000007#defineINF 1000000001#defineMAXN 10000intA[n];ll Dp[n];intMain () {intN=read (); a[1]=0;  for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); }dp[1]=0;  for(intI=2; i<=n;i++) {       if(a[i]>=a[i-1]&&a[i-1]>=a[i-2]) dp[i]=dp[i-1]+a[i]-a[i-1]; Else if(a[i]<=a[i-1]&&a[i-1]<=a[i-2]) dp[i]=dp[i-1]+a[i-1]-A[i]; Else if(a[i]>=a[i-1]&&a[i-1]<=a[i-2]) Dp[i]=max (dp[i-2]+a[i]-a[i-1],dp[i-1]); Else if(a[i]<=a[i-1]&&a[i-1]>=a[i-2]) Dp[i]=max (dp[i-2]+a[i-1]-a[i],dp[i-1]); } cout<<dp[n]<<Endl; return 0;}
Code

Codeforces Round #276 (Div. 1) D.kindergarten DP greedy

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.