Greedy observation +DP decision.
First, we need to observe a conclusion: each segment after segmentation must be monotonically increasing or monotonically decreasing.
You can then decide how to split the most value based on DP.
Dp[i][0] means that the last paragraph is the maximum value of the descending condition.
DP[I][1] means that the last paragraph is the maximum value in case of an increment
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;Const intmaxn=1e6+Ten;Long Longdp[maxn][2];Long LongA[MAXN];intN;intMain () {scanf ("%d",&N); a[0]=0; dp[1][1]=dp[1][0]=0; for(intI=1; i<=n;i++) scanf ("%lld",&A[i]); for(intI=2; i<=n;i++) { if(a[i]>a[i-1]) {dp[i][1]=max (dp[i-1][1]+a[i]-a[i-1],dp[i-1][0]); dp[i][0]=max (dp[i-1][0],dp[i-1][1]); } Else if(a[i]<a[i-1]) {dp[i][0]=max (dp[i-1][0]+a[i-1]-a[i],dp[i-1][1]); dp[i][1]=max (dp[i-1][0],dp[i-1][1]); } Else{dp[i][1]=dp[i][0]=max (dp[i-1][0],dp[i-1][1]); }} printf ("%lld\n", Max (dp[n][1],dp[n][0])); return 0;}
Codeforces 484D Kindergarten