codeforces788A Functions again

来源:互联网 发布:javascript if 编辑:程序博客网 时间:2024/06/07 00:47

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:

In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.

Input

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
51 4 2 3 1
output
3
input
41 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<cstdio>#include<string>#include<cstring>#include<algorithm>using namespace std;long long a[100001];long long s[100001];long long ss[100001];inline long long absx(long long x){if(x<0)x=-x;return x;}int main(){int n;scanf("%d",&n);int i;for(i=1;i<=n;i++)scanf("%I64d",&a[i]);for(i=1;i<=n-1;i++){s[i]=a[i+1]-a[i];if(s[i]<0)s[i]=-s[i];}long long ans=0;long long la=0,f=1;for(i=1;i<=n-1;i++){if(la<=0)la=s[i]*f;elsela=la+s[i]*f;ans=max(ans,la);f=-f;}la=0;f=-1;for(i=1;i<=n-1;i++){if(la<=0)la=s[i]*f;elsela=la+s[i]*f;ans=max(ans,la);f=-f;}printf("%I64d\n",ans);return 0;}


0 0