Something happened in Uzhlyandia again… There are riots on the streets… Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a.
The second line contains n integers a1, a2, …, an (-109 ≤ ai ≤ 109) — the array elements.
Output
Print the only integer — the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
题目描述;先让奇数取反,偶数不变,求出最大连续子序列,在让奇数取反,求出最大连续子序列,最后求二者的最大值
解题思路:问题挺简单,先取反,然后正常去求就可以了,下次求的时候,在上次的基础上再次去反就可以了,这样奇数就变回来,偶数也取反了
#include <iostream>
#include <cmath>
using namespace std;
int a[100015];
int main()
{
int x;
cin>>x;
for(long long i=1;i<=x;i++)
{
cin>>a[i];
}
for(long long i=1; i<x; i++)
a[i]=abs(a[i]-a[i+1]);
long long max1=0,max2=0,maxn;
for(long long i=1;i<x;i++)
{
if(i%2==1)
a[i]=-a[i];
}
max1=maxn=a[1];
for(long long i=2;i<x;i++)
{
if(maxn<=0)
maxn=a[i];
else
maxn+=a[i];
max1=max(max1,maxn);
}
for(long long i=1;i<x;i++)
{
a[i]=-a[i];
}
max2=maxn=a[1];
for(long long i=2;i<x;i++)
{
if(maxn<=0)
maxn=a[i];
else
maxn+=a[i];
max2=max(max2,maxn);
}
long long maxnx=max(max1,max2);
cout<<maxnx<<endl;
return 0;
}