若i-1,i,i+1不是呈单调不下降或单调不上升,i比两边高,就将i降到max(i-1,i+1),i比两边低,则降到min(i-1,i+1)
#include
#include
using namespace std;
int n,a[100010];
long long ans;
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
a[0]=a[1];
for (int i=1;i<=n;i++)
{
if (a[i-1]<=a[i])
{
ans+=a[i]-a[i-1];
if (a[i]>a[i+1]) a[i]=max(a[i-1],a[i+1]);
continue;
}
ans+=a[i-1]-a[i];
if (a[i]<a[i+1]) a[i]=min(a[i-1],a[i+1]);
}
printf("%lld",ans);
return 0;
}