///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<int,int>
#define                           node(a,b,c)                 mp(mp(a,b),c)
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=1e18;
const int mod=1e9+7;
const int M=1000005;
int dp[M][2];
int typ[M],a[M];
int yo(int x)
{
    return x?(x>0?1:-1):0;
}
 main()

{
    fast
    int n;
    cin>>n;
    f(i,1,n)cin>>a[i];
    f(i,2,n)
    {
        typ[i]=yo(a[i]-a[i-1]);
        int gap=abs(a[i]-a[i-1]);
        if(typ[i]!=0 && typ[i]==typ[i-1])
        {
           dp[i][1]=dp[i-1][1]+gap;
           dp[i][0]=dp[i-1][1];
        }
        else
        {
           dp[i][1]=max(dp[i-1][1],dp[i-1][0]+gap);
           dp[i][0]=dp[i-1][1];
        }
    }
    int ses=max(dp[n][0],dp[n][1]);
    cout<<ses<<"\n";
    return 0;

}


///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<int,int>
#define                           node(a,b,c)                 mp(mp(a,b),c)
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=1e18;
const int mod=1e9+7;
const int M=100005;
int dp[M][2];
int typ[M],a[M];
 main()

{
    fast
    int n;
    cin>>n;
    f(i,1,n)cin>>a[i];
    f(i,2,n)
    {
        typ[i]=a[i]-a[i-1];
        if(typ[i]<0)typ[i]=-1;
        else if(typ[i]>0)typ[i]=1;
        int gap=abs(a[i]-a[i-1]);
        if(typ[i]!=0 && typ[i]==typ[i-1])
        {
           dp[i][1]=dp[i-1][1]+gap;
           dp[i][0]=dp[i-1][1];
        }
        else
        {
           dp[i][1]=max(dp[i-1][1],dp[i-1][0]+gap);
           dp[i][0]=dp[i-1][1];
        }
    }
    int ses=max(dp[n][0],dp[n][1]);
    cout<<ses<<"\n";
    return 0;

}


///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<int,int>
#define                           node(a,b,c)                 mp(mp(a,b),c)
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=1e18;
const int mod=1e9+7;
const int M=1000005;
int dp[M][2];
int typ[M],a[M];
 main()

{
    fast
    int n;
    cin>>n;
    f(i,1,n)cin>>a[i];
    f(i,2,n)
    {
        typ[i]=a[i]-a[i-1];
        if(typ[i]<0)typ[i]=-1;
        else if(typ[i]>0)typ[i]=1;
        int gap=abs(a[i]-a[i-1]);
        if(typ[i]!=0 && typ[i]==typ[i-1])
        {
           dp[i][1]=dp[i-1][1]+gap;
           dp[i][0]=dp[i-1][1];
        }
        else
        {
           dp[i][1]=max(dp[i-1][1],dp[i-1][0]+gap);
           dp[i][0]=dp[i-1][1];
        }
    }
    int ses=max(dp[n][0],dp[n][1]);
    cout<<ses<<"\n";
    return 0;

}


/*And I thought my jokes were bad*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg puts("Viva la vida");
#define CHECK(x) cout << (#x) << " is " << (x) << endl;
#define nl puts("");
#define mp make_pair
#define IN(x) scanf("%d",&x);
#define INL(x) scanf("%lld",&x);
#define OUT(x) printf("%d",x);
#define OUTL(x) printf("%lld",x);
#define MAX 1000005
#define MOD 1000000009
#define INF (1 << 31)
#define CASE(x,y) printf("Case %d: %d\n",++x,y);
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define PI acos(-1.0)
#define EPS 1e-19
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define int ll
/*
	When I get older
	I will be stronger
	they'll call me freedom
	just like a waving flag
*/
int32_t arr[MAX],val[MAX],cnt;
map<int32_t,int32_t> idx;
int tre[4*MAX];
int dp[MAX];
void init(int32_t n,int32_t l,int32_t r)
{
	if(l==r)
	{
		tre[n]=-100000000000000000;
		return ;
	}
	int32_t mid=(l+r)/2;
	init(2*n,l,mid);
	init(2*n+1,mid+1,r);
	tre[n]=max(tre[2*n],tre[2*n+1]);
}
void update(int32_t n,int32_t l,int32_t r,int32_t i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre[n]=val;
		return ;
	}
	int mid=(l+r)/2;
	update(2*n,l,mid,i,val);
	update(2*n+1,mid+1,r,i,val);
	tre[n]=max(tre[2*n],tre[2*n+1]);
}
int query(int32_t n,int32_t l,int32_t r,int32_t i,int32_t j)
{
	if(i>r||j<l)
	{
		return -100000000000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre[n];
	}
	int32_t mid=(l+r)/2;
	return max(query(2*n,l,mid,i,j),query(2*n+1,mid+1,r,i,j));
}
int tre2[4*MAX];
void init2(int32_t n,int32_t l,int32_t r)
{
	if(l==r)
	{
		tre2[n]=-10000000000;
		return ;
	}
	int32_t mid=(l+r)/2;
	init2(2*n,l,mid);
	init2(2*n+1,mid+1,r);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
void update2(int32_t n,int32_t l,int32_t r,int32_t i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre2[n]=val;
		return ;
	}
	int32_t mid=(l+r)/2;
	update2(2*n,l,mid,i,val);
	update2(2*n+1,mid+1,r,i,val);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
int query2(int32_t n,int32_t l,int32_t r,int32_t i,int32_t j)
{
	if(i>r||j<l)
	{
		return -1000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre2[n];
	}
	int32_t mid=(l+r)/2;
	return max(query2(2*n,l,mid,i,j),query2(2*n+1,mid+1,r,i,j));
}

void solve()
{
	int i,j,k,l,r,m,n,a,b,c,d,x,y,z,t;
	scanf("%lld",&n);
	set<int> st;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		st.insert(arr[i]);
	}
	for(auto &cv:st)
	{
		idx[cv]=cnt;
		val[cnt++]=cv;
	}
	init(1,0,cnt-1);
	init2(1,0,cnt-1);
	for(i=0;i<n;i++)
	{
		int pre;
		if(i==0)
			pre=0;
		else
			pre=dp[i-1];
		x=tre[1]+arr[i];
		y=tre2[1]-arr[i];
		dp[i]=max(x,y);
		update(1,0,cnt-1,idx[arr[i]],pre-arr[i]);
		update2(1,0,cnt-1,idx[arr[i]],pre+arr[i]);
		dp[i]=max(dp[i],pre);
	}
	printf("%lld\n",dp[n-1]);

}
int32_t main()
{
	// FASTIO
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}


/*And I thought my jokes were bad*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define dbg puts("Viva la vida");
#define CHECK(x) cout << (#x) << " is " << (x) << endl;
#define nl puts("");
#define mp make_pair
#define IN(x) scanf("%d",&x);
#define INL(x) scanf("%lld",&x);
#define OUT(x) printf("%d",x);
#define OUTL(x) printf("%lld",x);
#define MAX 1000005
#define MOD 1000000009
#define INF (1 << 31)
#define CASE(x,y) printf("Case %d: %d\n",++x,y);
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define PI acos(-1.0)
#define EPS 1e-19
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define int ll
typedef cc_hash_table<int32_t, int32_t, hash<int32_t>> ht;
/*
	When I get older
	I will be stronger
	they'll call me freedom
	just like a waving flag
*/
int32_t arr[MAX],val[MAX],cnt;
ht idx;
int tre[4*MAX];
int tre2[4*MAX];
int dp[MAX];
void init(int32_t n,int32_t l,int32_t r)
{
	if(l==r)
	{
		tre2[n]=tre[n]=-100000000000000000;
		return ;
	}
	int32_t mid=(l+r)/2;
	init(2*n,l,mid);
	init(2*n+1,mid+1,r);
	tre2[n]=tre[n]=max(tre[2*n],tre[2*n+1]);
}
void update(int32_t n,int32_t l,int32_t r,int32_t i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre[n]=val;
		return ;
	}
	int mid=(l+r)/2;
	update(2*n,l,mid,i,val);
	update(2*n+1,mid+1,r,i,val);
	tre[n]=max(tre[2*n],tre[2*n+1]);
}
int query(int32_t n,int32_t l,int32_t r,int32_t i,int32_t j)
{
	if(i>r||j<l)
	{
		return -100000000000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre[n];
	}
	int32_t mid=(l+r)/2;
	return max(query(2*n,l,mid,i,j),query(2*n+1,mid+1,r,i,j));
}
void init2(int32_t n,int32_t l,int32_t r)
{
	if(l==r)
	{
		tre2[n]=-10000000000;
		return ;
	}
	int32_t mid=(l+r)/2;
	init2(2*n,l,mid);
	init2(2*n+1,mid+1,r);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
void update2(int32_t n,int32_t l,int32_t r,int32_t i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre2[n]=val;
		return ;
	}
	int32_t mid=(l+r)/2;
	update2(2*n,l,mid,i,val);
	update2(2*n+1,mid+1,r,i,val);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
int query2(int32_t n,int32_t l,int32_t r,int32_t i,int32_t j)
{
	if(i>r||j<l)
	{
		return -1000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre2[n];
	}
	int32_t mid=(l+r)/2;
	return max(query2(2*n,l,mid,i,j),query2(2*n+1,mid+1,r,i,j));
}

void solve()
{
	int i,j,k,l,r,m,n,a,b,c,d,x,y,z,t;
	scanf("%lld",&n);
	set<int> st;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		st.insert(arr[i]);
	}
	for(auto &cv:st)
	{
		idx[cv]=cnt;
		val[cnt++]=cv;
	}
	init(1,0,cnt-1);
	for(i=0;i<n;i++)
	{
		int pre;
		if(i==0)
			pre=0;
		else
			pre=dp[i-1];
		x=tre[1]+arr[i];
		y=tre2[1]-arr[i];
		dp[i]=max(x,y);
		update(1,0,cnt-1,idx[arr[i]],pre-arr[i]);
		update2(1,0,cnt-1,idx[arr[i]],pre+arr[i]);
		dp[i]=max(dp[i],pre);
	}
	printf("%lld\n",dp[n-1]);

}
int32_t main()
{
	// FASTIO
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}


/*And I thought my jokes were bad*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg puts("Viva la vida");
#define CHECK(x) cout << (#x) << " is " << (x) << endl;
#define nl puts("");
#define mp make_pair
#define IN(x) scanf("%d",&x);
#define INL(x) scanf("%lld",&x);
#define OUT(x) printf("%d",x);
#define OUTL(x) printf("%lld",x);
#define MAX 2000005
#define MOD 1000000009
#define INF (1 << 31)
#define CASE(x,y) printf("Case %d: %d\n",++x,y);
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define PI acos(-1.0)
#define EPS 1e-19
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define int ll
/*
	When I get older
	I will be stronger
	they'll call me freedom
	just like a waving flag
*/
int arr[MAX],val[MAX],cnt;
map<int,int> idx;
int tre[4*MAX];
int dp[MAX];
void init(int n,int l,int r)
{
	if(l==r)
	{
		tre[n]=-10000000000;
		return ;
	}
	int mid=(l+r)/2;
	init(2*n,l,mid);
	init(2*n+1,mid+1,r);
	tre[n]=max(tre[2*n],tre[2*n+1]);
}
void update(int n,int l,int r,int i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre[n]=val;
		return ;
	}
	int mid=(l+r)/2;
	update(2*n,l,mid,i,val);
	update(2*n+1,mid+1,r,i,val);
	tre[n]=max(tre[2*n],tre[2*n+1]);
}
int query(int n,int l,int r,int i,int j)
{
	if(i>r||j<l)
	{
		return -1000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre[n];
	}
	int mid=(l+r)/2;
	return max(query(2*n,l,mid,i,j),query(2*n+1,mid+1,r,i,j));
}
int tre2[4*MAX];
void init2(int n,int l,int r)
{
	if(l==r)
	{
		tre2[n]=-10000000000;
		return ;
	}
	int mid=(l+r)/2;
	init2(2*n,l,mid);
	init2(2*n+1,mid+1,r);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
void update2(int n,int l,int r,int i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre2[n]=val;
		return ;
	}
	int mid=(l+r)/2;
	update2(2*n,l,mid,i,val);
	update2(2*n+1,mid+1,r,i,val);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
int query2(int n,int l,int r,int i,int j)
{
	if(i>r||j<l)
	{
		return -1000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre2[n];
	}
	int mid=(l+r)/2;
	return max(query2(2*n,l,mid,i,j),query2(2*n+1,mid+1,r,i,j));
}

void solve()
{
	int i,j,k,l,r,m,n,a,b,c,d,x,y,z,t;
	cin>>n;
	set<int> st;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		st.insert(arr[i]);
	}
	for(auto &cv:st)
	{
		idx[cv]=cnt;
		val[cnt++]=cv;
	}
	init(1,0,cnt-1);
	init2(1,0,cnt-1);
	for(i=0;i<n;i++)
	{
		int pre;
		if(i==0)
			pre=0;
		else
			pre=dp[i-1];
		x=query(1,0,cnt-1,0,cnt-1)+arr[i];
		y=query2(1,0,cnt-1,0,cnt-1)-arr[i];
		dp[i]=max(x,y);
		update(1,0,cnt-1,idx[arr[i]],pre-arr[i]);
		update2(1,0,cnt-1,idx[arr[i]],pre+arr[i]);
		dp[i]=max(dp[i],pre);
	}
	cout<<dp[n-1]<<endl;

}
int32_t main()
{
	FASTIO
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}


/*And I thought my jokes were bad*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg puts("Viva la vida");
#define CHECK(x) cout << (#x) << " is " << (x) << endl;
#define nl puts("");
#define mp make_pair
#define IN(x) scanf("%d",&x);
#define INL(x) scanf("%lld",&x);
#define OUT(x) printf("%d",x);
#define OUTL(x) printf("%lld",x);
#define MAX 1000005
#define MOD 1000000009
#define INF (1 << 31)
#define CASE(x,y) printf("Case %d: %d\n",++x,y);
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define PI acos(-1.0)
#define EPS 1e-19
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define int ll
/*
	When I get older
	I will be stronger
	they'll call me freedom
	just like a waving flag
*/
int32_t arr[MAX],val[MAX],cnt;
unordered_map<int32_t,int32_t> idx;
int tre[4*MAX];
int tre2[4*MAX];
int dp[MAX];
void init(int32_t n,int32_t l,int32_t r)
{
	if(l==r)
	{
		tre2[n]=tre[n]=-100000000000000000;
		return ;
	}
	int32_t mid=(l+r)/2;
	init(2*n,l,mid);
	init(2*n+1,mid+1,r);
	tre2[n]=tre[n]=max(tre[2*n],tre[2*n+1]);
}
void update(int32_t n,int32_t l,int32_t r,int32_t i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre[n]=val;
		return ;
	}
	int mid=(l+r)/2;
	update(2*n,l,mid,i,val);
	update(2*n+1,mid+1,r,i,val);
	tre[n]=max(tre[2*n],tre[2*n+1]);
}
int query(int32_t n,int32_t l,int32_t r,int32_t i,int32_t j)
{
	if(i>r||j<l)
	{
		return -100000000000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre[n];
	}
	int32_t mid=(l+r)/2;
	return max(query(2*n,l,mid,i,j),query(2*n+1,mid+1,r,i,j));
}
void init2(int32_t n,int32_t l,int32_t r)
{
	if(l==r)
	{
		tre2[n]=-10000000000;
		return ;
	}
	int32_t mid=(l+r)/2;
	init2(2*n,l,mid);
	init2(2*n+1,mid+1,r);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
void update2(int32_t n,int32_t l,int32_t r,int32_t i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre2[n]=val;
		return ;
	}
	int32_t mid=(l+r)/2;
	update2(2*n,l,mid,i,val);
	update2(2*n+1,mid+1,r,i,val);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
int query2(int32_t n,int32_t l,int32_t r,int32_t i,int32_t j)
{
	if(i>r||j<l)
	{
		return -1000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre2[n];
	}
	int32_t mid=(l+r)/2;
	return max(query2(2*n,l,mid,i,j),query2(2*n+1,mid+1,r,i,j));
}

void solve()
{
	int i,j,k,l,r,m,n,a,b,c,d,x,y,z,t;
	scanf("%lld",&n);
	set<int> st;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		st.insert(arr[i]);
	}
	for(auto &cv:st)
	{
		idx[cv]=cnt;
		val[cnt++]=cv;
	}
	init(1,0,cnt-1);
	for(i=0;i<n;i++)
	{
		int pre;
		if(i==0)
			pre=0;
		else
			pre=dp[i-1];
		x=tre[1]+arr[i];
		y=tre2[1]-arr[i];
		dp[i]=max(x,y);
		update(1,0,cnt-1,idx[arr[i]],pre-arr[i]);
		update2(1,0,cnt-1,idx[arr[i]],pre+arr[i]);
		dp[i]=max(dp[i],pre);
	}
	printf("%lld\n",dp[n-1]);

}
int32_t main()
{
	// FASTIO
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}


/*And I thought my jokes were bad*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define dbg puts("Viva la vida");
#define CHECK(x) cout << (#x) << " is " << (x) << endl;
#define nl puts("");
#define mp make_pair
#define IN(x) scanf("%d",&x);
#define INL(x) scanf("%lld",&x);
#define OUT(x) printf("%d",x);
#define OUTL(x) printf("%lld",x);
#define MAX 1000005
#define MOD 1000000009
#define INF (1 << 31)
#define CASE(x,y) printf("Case %d: %d\n",++x,y);
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define PI acos(-1.0)
#define EPS 1e-19
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define int ll
typedef cc_hash_table<int32_t, int32_t, hash<int32_t>> ht;
/*
	When I get older
	I will be stronger
	they'll call me freedom
	just like a waving flag
*/
int32_t arr[MAX],val[MAX],cnt;
ht idx;
int dp[MAX];
int N;
int tre[2 * MAX];
int combine(int x,int y)
{
	return max(x,y);
}
void build() {
  for (int i = N - 1; i > 0; --i) tre[i] = combine(tre[i<<1] , tre[i<<1|1]);
}

void modify(int p, int value) {
  for (tre[p += N] = value; p > 1; p >>= 1) tre[p>>1] = combine(tre[p],tre[p^1]);
}

int query(int l, int r) {
  int res = -1000000000000;
  for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
    if (l&1) res = combine(res,tre[l++]);
    if (r&1) res = combine(res,tre[--r]);
  }
  return res;
}
int tre2[2 * MAX];

void build2() {
  for (int i = N - 1; i > 0; --i) tre2[i] = combine(tre2[i<<1] , tre2[i<<1|1]);
}

void modify2(int p, int value) {
  for (tre2[p += N] = value; p > 1; p >>= 1) tre2[p>>1] = combine(tre2[p],tre2[p^1]);
}

int query2(int l, int r) {
  int res = -1000000000000;
  for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
    if (l&1) res = combine(res,tre2[l++]);
    if (r&1) res = combine(res,tre2[--r]);
  }
  return res;
}

void solve()
{
	int i,j,k,l,r,m,n,a,b,c,d,x,y,z,t;
	scanf("%lld",&n);
	set<int> st;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		st.insert(arr[i]);
	}
	for(auto &cv:st)
	{
		tre[st.size()+cnt]=tre2[st.size()+cnt]=-1000000000000;
		idx[cv]=cnt;
		val[cnt++]=cv;
	}
	N=cnt;
	build2();
	build();
	for(i=0;i<n;i++)
	{
		int pre;
		if(i==0)
			pre=0;
		else
			pre=dp[i-1];
		x=query(0,N)+arr[i];
		y=query2(0,N)-arr[i];
		dp[i]=max(x,y);
		modify(idx[arr[i]],pre-arr[i]);
		modify2(idx[arr[i]],pre+arr[i]);
		dp[i]=max(dp[i],pre);
	}
	printf("%lld\n",dp[n-1]);

}
int32_t main()
{
	// FASTIO
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}


/*And I thought my jokes were bad*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg puts("Viva la vida");
#define CHECK(x) cout << (#x) << " is " << (x) << endl;
#define nl puts("");
#define mp make_pair
#define IN(x) scanf("%d",&x);
#define INL(x) scanf("%lld",&x);
#define OUT(x) printf("%d",x);
#define OUTL(x) printf("%lld",x);
#define MAX 2000005
#define MOD 1000000009
#define INF (1 << 31)
#define CASE(x,y) printf("Case %d: %d\n",++x,y);
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define PI acos(-1.0)
#define EPS 1e-19
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define int ll
/*
	When I get older
	I will be stronger
	they'll call me freedom
	just like a waving flag
*/
int arr[MAX],val[MAX],cnt;
map<int,int> idx;
int tre[4*MAX];
int dp[MAX];
void init(int n,int l,int r)
{
	if(l==r)
	{
		tre[n]=-100000000000000000;
		return ;
	}
	int mid=(l+r)/2;
	init(2*n,l,mid);
	init(2*n+1,mid+1,r);
	tre[n]=max(tre[2*n],tre[2*n+1]);
}
void update(int n,int l,int r,int i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre[n]=val;
		return ;
	}
	int mid=(l+r)/2;
	update(2*n,l,mid,i,val);
	update(2*n+1,mid+1,r,i,val);
	tre[n]=max(tre[2*n],tre[2*n+1]);
}
int query(int n,int l,int r,int i,int j)
{
	if(i>r||j<l)
	{
		return -100000000000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre[n];
	}
	int mid=(l+r)/2;
	return max(query(2*n,l,mid,i,j),query(2*n+1,mid+1,r,i,j));
}
int tre2[4*MAX];
void init2(int n,int l,int r)
{
	if(l==r)
	{
		tre2[n]=-10000000000;
		return ;
	}
	int mid=(l+r)/2;
	init2(2*n,l,mid);
	init2(2*n+1,mid+1,r);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
void update2(int n,int l,int r,int i,int val)
{
	if(i<l||i>r)
	{
		return ;
	}
	if(l==r)
	{
		tre2[n]=val;
		return ;
	}
	int mid=(l+r)/2;
	update2(2*n,l,mid,i,val);
	update2(2*n+1,mid+1,r,i,val);
	tre2[n]=max(tre2[2*n],tre2[2*n+1]);
}
int query2(int n,int l,int r,int i,int j)
{
	if(i>r||j<l)
	{
		return -1000000000;
	}
	if(i<=l&&j>=r)
	{
		return tre2[n];
	}
	int mid=(l+r)/2;
	return max(query2(2*n,l,mid,i,j),query2(2*n+1,mid+1,r,i,j));
}

void solve()
{
	int i,j,k,l,r,m,n,a,b,c,d,x,y,z,t;
	scanf("%lld",&n);
	set<int> st;
	for(i=0;i<n;i++)
	{
		scanf("%lld",&arr[i]);
		st.insert(arr[i]);
	}
	for(auto &cv:st)
	{
		idx[cv]=cnt;
		val[cnt++]=cv;
	}
	init(1,0,cnt-1);
	init2(1,0,cnt-1);
	for(i=0;i<n;i++)
	{
		int pre;
		if(i==0)
			pre=0;
		else
			pre=dp[i-1];
		x=tre[1]+arr[i];
		y=tre2[1]-arr[i];
		dp[i]=max(x,y);
		update(1,0,cnt-1,idx[arr[i]],pre-arr[i]);
		update2(1,0,cnt-1,idx[arr[i]],pre+arr[i]);
		dp[i]=max(dp[i],pre);
	}
	printf("%lld\n",dp[n-1]);

}
int32_t main()
{
	// FASTIO
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define x first
#define y second
typedef long long ll;

typedef pair<ll, ll> pii;
const int N = 1000010;
const int OFFSET = 10000;
const int LOGN = 20;
const int oo = 1e9;
const int MOD = 1000000007;

int n , a[N],dp[N],mx[N][LOGN],mn[N][LOGN],lg[N];
bool v[N];
int r1(int l , int r){
  r = min(r,n-1);
  int fl = lg[r-l+1];
  return min(mn[l][fl],mn[r-(1<<fl)+1][fl]);
}
int r2(int l , int r){
  r = min(r,n-1);
  int fl = lg[r-l+1];
  return max(mx[l][fl],mx[r-(1<<fl)+1][fl]);
}
int main(){
  cin >> n;
  v[0] = v[n-1] = 1;
  lg[1] = 0;
  for(int i = 2 ; i <= n ; ++i)lg[i] = 1+lg[i/2];
for (int i = 0; i < n; ++i)
    scanf("%d",&a[i]);
  for (int i = 0; i < n; ++i){
    if(i != n-1 && i && ((a[i]>a[i-1] && a[i]>a[i+1])|| (a[i]<a[i-1] && a[i] < a[i+1]))){
      v[i] = v[i-1]=v[i+1] = 1;
    }
    mx[i][0] = mn[i][0] = a[i];
  }
  for(int j = 1 ; j < LOGN; ++j)
    for(int i = 0 ; i + (1 << j)-1 < n;++i){
      mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
      mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
    }

  deque<int> bst;
  //bst.push_back(n);
  for(int i = n-1 ; i >= 0 ; --i){
    if(v[i])bst.push_front(i);
    while(bst.size() > 10)bst.pop_back();
    for(auto j:bst){
      dp[i] = max(dp[i],r2(i,j)-r1(i,j)+dp[j+1]);
    }
    dp[i] = max(dp[i+1],dp[i]);
  }
  cout << dp[0] << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define x first
#define y second
typedef long long ll;

typedef pair<ll, ll> pii;
const int N = 1000010;

int a[N],n;
ll dp[N][3];

int main(){
  cin >> n;
  for(int i = 0 ; i < n ; ++i)scanf("%d",a+i);
  dp[n][1]=dp[n][2] = -1e18;
  for(int i = n-1 ; i >= 0 ; --i)
    for(int k = 0 ; k <= 2 ; ++k){
      ll &ret = dp[i][k];
      if(k==0)
        ret = max(dp[i+1][k],max(-a[i]+dp[i+1][1],a[i]+dp[i+1][2]));
      else if(k==1)
        ret = max(dp[i+1][k],+a[i]+dp[i+1][0]);
      else
        ret = max(dp[i+1][k],-a[i]+dp[i+1][0]);
    }
  printf("%lld\n",dp[0][0] );

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define x first
#define y second
typedef long long ll;

typedef pair<ll, ll> pii;
const int N = 1000010;
const int OFFSET = 10000;
const int LOGN = 20;
const int oo = 1e9;
const int MOD = 1000000007;

int n , a[N],dp[N],mx[N][LOGN],mn[N][LOGN],lg[N];
bool v[N];
int r1(int l , int r){
  r = min(r,n-1);
  int fl = lg[r-l+1];
  return min(mn[l][fl],mn[r-(1<<fl)+1][fl]);
}
int r2(int l , int r){
  r = min(r,n-1);
  int fl = lg[r-l+1];
  return max(mx[l][fl],mx[r-(1<<fl)+1][fl]);
}
int main(){
  cin >> n;
  v[0] = v[n-1] = 1;
  lg[1] = 0;
  for(int i = 2 ; i <= n ; ++i)lg[i] = 1+lg[i/2];
  for (int i = 0; i < n; ++i){
    scanf("%d",&a[i]);
    if(i != n-1 && i && ((a[i]>a[i-1] && a[i]>a[i+1])|| (a[i]<a[i-1] && a[i] < a[i+1]))){
      v[i] = v[i-1]=v[i+1] = 1;
    }
    mx[i][0] = mn[i][0] = a[i];
  }
  for(int j = 1 ; j < LOGN; ++j)
    for(int i = 0 ; i + (1 << j)-1 < n;++i){
      mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
      mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
    }

  deque<int> bst;
  bst.push_back(n);
  for(int i = n-1 ; i >= 0 ; --i){
    if(v[i])bst.push_front(i);
    while(bst.size() > 10)bst.pop_back();
    for(auto j:bst){
      dp[i] = max(dp[i],r2(i,j)-r1(i,j)+dp[j+1]);
    }
  }
  cout << dp[0] << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define x first
#define y second
typedef long long ll;

typedef pair<ll, ll> pii;
const int N = 1000010;
const int OFFSET = 10000;
const int LOGN = 20;
const int oo = 1e9;
const int MOD = 1000000007;

int n , a[N],dp[N],mx[N][LOGN],mn[N][LOGN],lg[N];
bool v[N];
int r1(int l , int r){
  r = min(r,n-1);
  int fl = lg[r-l+1];
  return min(mn[l][fl],mn[r-(1<<fl)+1][fl]);
}
int r2(int l , int r){
  r = min(r,n-1);
  int fl = lg[r-l+1];
  return max(mx[l][fl],mx[r-(1<<fl)+1][fl]);
}
int main(){
  cin >> n;
  v[0] = v[n-1] = 1;
  lg[1] = 0;
  for(int i = 2 ; i <= n ; ++i)lg[i] = 1+lg[i/2];
  for (int i = 0; i < n; ++i){
    scanf("%d",&a[i]);
    if(i != n-1 && i && ((a[i]>a[i-1] && a[i]>a[i+1])|| (a[i]<a[i-1] && a[i] < a[i+1]))){
      v[i] = v[i-1]=v[i+1] = 1;
    }
    mx[i][0] = mn[i][0] = a[i];
  }
  for(int j = 1 ; j < LOGN; ++j)
    for(int i = 0 ; i + (1 << j)-1 < n;++i){
      mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
      mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
    }

  deque<int> bst;
  bst.push_back(n);
  for(int i = n-1 ; i >= 0 ; --i){
    if(v[i])bst.push_front(i);
    while(bst.size() > 10)bst.pop_back();
    for(auto j:bst){
      dp[i] = max(dp[i],r2(i,j)-r1(i,j)+dp[j+1]);
    }
    dp[i] = max(dp[i+1],dp[i]);
  }
  cout << dp[0] << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define x first
#define y second
typedef long long ll;

typedef pair<ll, ll> pii;
const int N = 1000010;
const int OFFSET = 10000;
const int LOGN = 20;
const int oo = 1e9;
const int MOD = 1000000007;

int n , a[N],mx[N][LOGN],mn[N][LOGN],lg[N];
ll dp[N];
bool v[N];
int r1(int l , int r){
  r = min(r,n-1);
  int fl = lg[r-l+1];
  return min(mn[l][fl],mn[r-(1<<fl)+1][fl]);
}
int r2(int l , int r){
  r = min(r,n-1);
  int fl = lg[r-l+1];
  return max(mx[l][fl],mx[r-(1<<fl)+1][fl]);
}
int main(){
  cin >> n;
  v[0] = v[n-1] = 1;
  lg[1] = 0;
  for(int i = 2 ; i <= n ; ++i)lg[i] = 1+lg[i/2];
for (int i = 0; i < n; ++i)
    scanf("%d",&a[i]);
  for (int i = 0; i < n; ++i){
    if(i != n-1 && i && ((a[i]>a[i-1] && a[i]>a[i+1])|| (a[i]<a[i-1] && a[i] < a[i+1]))){
      v[i] = v[i-1]=v[i+1] = 1;
    }
    mx[i][0] = mn[i][0] = a[i];
  }
  for(int j = 1 ; j < LOGN; ++j)
    for(int i = 0 ; i + (1 << j)-1 < n;++i){
      mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
      mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
    }

  deque<int> bst;
  //bst.push_back(n);
  for(int i = n-1 ; i >= 0 ; --i){
    if(v[i])bst.push_front(i);
    while(bst.size() > 10)bst.pop_back();
    for(auto j:bst){
      dp[i] = max(dp[i],r2(i,j)-r1(i,j)+dp[j+1]);
    }
    dp[i] = max(dp[i+1],dp[i]);
  }
  cout << dp[0] << endl;

  return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1e9+7; // 998244353
const ll INF = 1e18;
const int MX = 1<<18;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

namespace input {
    template<class T> void sc(complex<T>& x);
    template<class T1, class T2> void sc(pair<T1,T2>& p);
    template<class T> void sc(vector<T>& a);
    template<class T, size_t SZ> void sc(array<T,SZ>& a);

    template<class T> void sc(T& x) { cin >> x; }
    void sc(double& x) { string t; sc(t); x = stod(t); }
    void sc(ld& x) { string t; sc(t); x = stold(t); }
    template<class Arg, class... Args> void sc(Arg& first, Args&... rest) {
        sc(first); sc(rest...);
    }

    template<class T> void sc(complex<T>& x) { T a,b; sc(a,b); x = cd(a,b); }
    template<class T1, class T2> void sc(pair<T1,T2>& p) { sc(p.f,p.s); }
    template<class T> void sc(vector<T>& a) { F0R(i,sz(a)) sc(a[i]); }
    template<class T, size_t SZ> void sc(array<T,SZ>& a) { F0R(i,SZ) sc(a[i]); }
}

using namespace input;

namespace output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T> void prContain(const T& x) {
        pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }

    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps(); // no space at end of line
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...); // print w/ spaces
    }
}

using namespace output;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

template<class T> T invGeneral(T a, T b) {
    a %= b; if (a == 0) return b == 1 ? 0 : -1;
    T x = invGeneral(b,a);
    return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
}

template<class T> struct modular {
    T val;
    explicit operator T() const { return val; }
    modular() { val = 0; }
    template<class U> modular(const U& v) {
        val = (-MOD <= v && v <= MOD) ? v : v % MOD;
        if (val < 0) val += MOD;
    }
    friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
    friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
    friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }

    modular operator-() const { return modular(-val); }
    modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
    modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
    modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
    friend modular exp(modular a, ll p) {
        modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans;
    }
    friend modular inv(const modular& a) { return invGeneral(a.val,MOD); }
    // inv is equivalent to return exp(b,b.mod-2) if prime
    modular& operator/=(const modular& m) { return (*this) *= inv(m); }

    friend modular operator+(modular a, const modular& b) { return a += b; }
    friend modular operator-(modular a, const modular& b) { return a -= b; }
    friend modular operator*(modular a, const modular& b) { return a *= b; }

    friend modular operator/(modular a, const modular& b) { return a /= b; }
};

typedef modular<int> mi;
typedef pair<mi,mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;

int a[MX];
ll dp[MX];

int main() {
    setIO();
    int n; sc(n);
    FOR(i,1,n+1) sc(a[i]);
    int p = 1;
    FOR(i,2,n+1) {
        ckmax(dp[i],dp[p-1]+abs(a[i]-a[p]));
        ckmax(dp[i],dp[p]+abs(a[i]-a[p+1]));
        if (a[i-1] <= a[i] && a[i] >= a[i+1]) p = i;
        if (a[i-1] >= a[i] && a[i] <= a[i+1]) p = i;
    }
    ps(dp[n]);
    return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1e9+7; // 998244353
const ll INF = 1e18;
const int MX = 1e6+6;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

namespace input {
    template<class T> void sc(complex<T>& x);
    template<class T1, class T2> void sc(pair<T1,T2>& p);
    template<class T> void sc(vector<T>& a);
    template<class T, size_t SZ> void sc(array<T,SZ>& a);

    template<class T> void sc(T& x) { cin >> x; }
    void sc(double& x) { string t; sc(t); x = stod(t); }
    void sc(ld& x) { string t; sc(t); x = stold(t); }
    template<class Arg, class... Args> void sc(Arg& first, Args&... rest) {
        sc(first); sc(rest...);
    }

    template<class T> void sc(complex<T>& x) { T a,b; sc(a,b); x = cd(a,b); }
    template<class T1, class T2> void sc(pair<T1,T2>& p) { sc(p.f,p.s); }
    template<class T> void sc(vector<T>& a) { F0R(i,sz(a)) sc(a[i]); }
    template<class T, size_t SZ> void sc(array<T,SZ>& a) { F0R(i,SZ) sc(a[i]); }
}

using namespace input;

namespace output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T> void prContain(const T& x) {
        pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }

    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps(); // no space at end of line
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...); // print w/ spaces
    }
}

using namespace output;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

template<class T> T invGeneral(T a, T b) {
    a %= b; if (a == 0) return b == 1 ? 0 : -1;
    T x = invGeneral(b,a);
    return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
}

template<class T> struct modular {
    T val;
    explicit operator T() const { return val; }
    modular() { val = 0; }
    template<class U> modular(const U& v) {
        val = (-MOD <= v && v <= MOD) ? v : v % MOD;
        if (val < 0) val += MOD;
    }
    friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
    friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
    friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }

    modular operator-() const { return modular(-val); }
    modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
    modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
    modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
    friend modular exp(modular a, ll p) {
        modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans;
    }
    friend modular inv(const modular& a) { return invGeneral(a.val,MOD); }
    // inv is equivalent to return exp(b,b.mod-2) if prime
    modular& operator/=(const modular& m) { return (*this) *= inv(m); }

    friend modular operator+(modular a, const modular& b) { return a += b; }
    friend modular operator-(modular a, const modular& b) { return a -= b; }
    friend modular operator*(modular a, const modular& b) { return a *= b; }

    friend modular operator/(modular a, const modular& b) { return a /= b; }
};

typedef modular<int> mi;
typedef pair<mi,mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;

int a[MX];
ll dp[MX];

int main() {
    setIO();
    int n; sc(n);
    FOR(i,1,n+1) sc(a[i]);
    int p = 1;
    FOR(i,2,n+1) {
        ckmax(dp[i],dp[p-1]+abs(a[i]-a[p]));
        ckmax(dp[i],dp[p]+abs(a[i]-a[p+1]));
        if (a[i-1] <= a[i] && a[i] >= a[i+1]) p = i;
        if (a[i-1] >= a[i] && a[i] <= a[i+1]) p = i;
    }
    ps(dp[n]);
    return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1e9+7; // 998244353
const ll INF = 1e18;
const int MX = 1<<18;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

namespace input {
    template<class T> void sc(complex<T>& x);
    template<class T1, class T2> void sc(pair<T1,T2>& p);
    template<class T> void sc(vector<T>& a);
    template<class T, size_t SZ> void sc(array<T,SZ>& a);

    template<class T> void sc(T& x) { cin >> x; }
    void sc(double& x) { string t; sc(t); x = stod(t); }
    void sc(ld& x) { string t; sc(t); x = stold(t); }
    template<class Arg, class... Args> void sc(Arg& first, Args&... rest) {
        sc(first); sc(rest...);
    }

    template<class T> void sc(complex<T>& x) { T a,b; sc(a,b); x = cd(a,b); }
    template<class T1, class T2> void sc(pair<T1,T2>& p) { sc(p.f,p.s); }
    template<class T> void sc(vector<T>& a) { F0R(i,sz(a)) sc(a[i]); }
    template<class T, size_t SZ> void sc(array<T,SZ>& a) { F0R(i,SZ) sc(a[i]); }
}

using namespace input;

namespace output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T> void prContain(const T& x) {
        pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }

    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps(); // no space at end of line
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...); // print w/ spaces
    }
}

using namespace output;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

template<class T> T invGeneral(T a, T b) {
    a %= b; if (a == 0) return b == 1 ? 0 : -1;
    T x = invGeneral(b,a);
    return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
}

template<class T> struct modular {
    T val;
    explicit operator T() const { return val; }
    modular() { val = 0; }
    template<class U> modular(const U& v) {
        val = (-MOD <= v && v <= MOD) ? v : v % MOD;
        if (val < 0) val += MOD;
    }
    friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
    friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
    friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }

    modular operator-() const { return modular(-val); }
    modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
    modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
    modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
    friend modular exp(modular a, ll p) {
        modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans;
    }
    friend modular inv(const modular& a) { return invGeneral(a.val,MOD); }
    // inv is equivalent to return exp(b,b.mod-2) if prime
    modular& operator/=(const modular& m) { return (*this) *= inv(m); }

    friend modular operator+(modular a, const modular& b) { return a += b; }
    friend modular operator-(modular a, const modular& b) { return a -= b; }
    friend modular operator*(modular a, const modular& b) { return a *= b; }

    friend modular operator/(modular a, const modular& b) { return a /= b; }
};

typedef modular<int> mi;
typedef pair<mi,mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;

int a[MX];
ll dp[MX];

int main() {
    setIO();
    int n; sc(n);
    FOR(i,1,n+1) sc(a[i]);
    int p = 1;
    FOR(i,2,n+1) {
        ckmax(dp[i],dp[p-1]+abs(a[i]-a[p]));
        ckmax(dp[i],dp[p]+abs(a[i]-a[p+1]));
        if (a[i-1] <= a[i] && a[i] >= a[i+1]) p = i;
        if (a[i-1] >= a[i] && a[i] <= a[i+1]) p = i;
    }
    ps(dp[n]);
    return 0;
}#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1e9+7; // 998244353
const ll INF = 1e18;
const int MX = 1e6+6;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

namespace input {
    template<class T> void sc(complex<T>& x);
    template<class T1, class T2> void sc(pair<T1,T2>& p);
    template<class T> void sc(vector<T>& a);
    template<class T, size_t SZ> void sc(array<T,SZ>& a);

    template<class T> void sc(T& x) { cin >> x; }
    void sc(double& x) { string t; sc(t); x = stod(t); }
    void sc(ld& x) { string t; sc(t); x = stold(t); }
    template<class Arg, class... Args> void sc(Arg& first, Args&... rest) {
        sc(first); sc(rest...);
    }

    template<class T> void sc(complex<T>& x) { T a,b; sc(a,b); x = cd(a,b); }
    template<class T1, class T2> void sc(pair<T1,T2>& p) { sc(p.f,p.s); }
    template<class T> void sc(vector<T>& a) { F0R(i,sz(a)) sc(a[i]); }
    template<class T, size_t SZ> void sc(array<T,SZ>& a) { F0R(i,SZ) sc(a[i]); }
}

using namespace input;

namespace output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T> void prContain(const T& x) {
        pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }

    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps(); // no space at end of line
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...); // print w/ spaces
    }
}

using namespace output;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

template<class T> T invGeneral(T a, T b) {
    a %= b; if (a == 0) return b == 1 ? 0 : -1;
    T x = invGeneral(b,a);
    return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
}

template<class T> struct modular {
    T val;
    explicit operator T() const { return val; }
    modular() { val = 0; }
    template<class U> modular(const U& v) {
        val = (-MOD <= v && v <= MOD) ? v : v % MOD;
        if (val < 0) val += MOD;
    }
    friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
    friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
    friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }

    modular operator-() const { return modular(-val); }
    modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
    modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
    modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
    friend modular exp(modular a, ll p) {
        modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans;
    }
    friend modular inv(const modular& a) { return invGeneral(a.val,MOD); }
    // inv is equivalent to return exp(b,b.mod-2) if prime
    modular& operator/=(const modular& m) { return (*this) *= inv(m); }

    friend modular operator+(modular a, const modular& b) { return a += b; }
    friend modular operator-(modular a, const modular& b) { return a -= b; }
    friend modular operator*(modular a, const modular& b) { return a *= b; }

    friend modular operator/(modular a, const modular& b) { return a /= b; }
};

typedef modular<int> mi;
typedef pair<mi,mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;

int a[MX];
ll dp[MX];

int main() {
    setIO();
    int n; sc(n);
    FOR(i,1,n+1) sc(a[i]);
    int p = 1;
    FOR(i,2,n+1) {
        ckmax(dp[i],dp[p-1]+abs(a[i]-a[p]));
        ckmax(dp[i],dp[p]+abs(a[i]-a[p+1]));
        if (a[i-1] <= a[i] && a[i] >= a[i+1]) p = i;
        if (a[i-1] >= a[i] && a[i] <= a[i+1]) p = i;
    }
    ps(dp[n]);
    return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HI cout<<"hii"<<endl;
#define get(x) cout<<x<<endl;
#define INF 1e18
using namespace std;
vector<int> v[1000005];
int cur=0,ans=0;
bool bb=false;

signed main()
{
    jaldi
    int n;
    cin>>n;
    int a[n],b[n]={},c[n]={};
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n-1;i++)
    {
        if(a[i]>=a[i+1]&&a[i]>=a[i-1]) b[i]=1;
        else if(a[i]<=a[i+1]&&a[i]<=a[i-1]) b[i]=1;
    }
    for(int i=0;i<n;i++) if(b[i]) {c[i]=1;c[i+1]=1;c[i-1]=1;}
    for(int i=0;i<n-1;i++) if(!b[i]&&!b[i+1]) ans+=abs(a[i+1]-a[i]);
    for(int i=0;i<n;i++)
    {
        if(!bb&&c[i]) bb=true;
        if(bb&&!c[i]) {bb=false;cur++;}
        if(bb) v[cur].push_back(a[i]);
        if(c[i]&&c[i+1]&&!b[i]&&!b[i+1]) {bb=false;cur++;}
    }
    for(int i=0;i<1000005;i++)
    {
        int sz=v[i].size();
        if(!sz) break;
        int t[sz+1]={};
        t[0]=0;
        t[1]=abs(v[i][0]-v[i][1]);
        for(int j=2;j<sz;j++)
        {
            t[j]=max(t[j-1],t[j-2]+abs(v[i][j]-v[i][j-1]));
        }
        ans+=t[sz-1];
    }
    get(ans)

	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HI cout<<"hii"<<endl;
#define get(x) cout<<x<<endl;
#define INF 1e18
using namespace std;
vector<int> v[1000005];
int cur=0,ans=0;
bool bb=false;

signed main()
{
    jaldi
    int n;
    cin>>n;
    int a[n],b[n]={},c[n]={};
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n-1;i++)
    {
        if(a[i]>=a[i+1]&&a[i]>=a[i-1]) b[i]=1;
        else if(a[i]<a[i+1]&&a[i]<a[i-1]) b[i]=1;
    }
    for(int i=0;i<n;i++) if(b[i]) {c[i]=1;c[i+1]=1;c[i-1]=1;}
    for(int i=0;i<n-1;i++) if(!b[i]&&!b[i+1]) ans+=abs(a[i+1]-a[i]);
    for(int i=0;i<n;i++)
    {
        if(!bb&&c[i]) bb=true;
        if(bb&&!c[i]) {bb=false;cur++;}
        if(bb) v[cur].push_back(a[i]);
        if(c[i]&&c[i+1]&&!b[i]&&!b[i+1]) {bb=false;cur++;}
    }
    for(int i=0;i<1000005;i++)
    {
        int sz=v[i].size();
        if(!sz) break;
        int t[sz+1]={};
        t[0]=0;
        t[1]=abs(v[i][0]-v[i][1]);
        for(int j=2;j<sz;j++)
        {
            t[j]=max(t[j-1],t[j-2]+abs(v[i][j]-v[i][j-1]));
        }
        ans+=t[sz-1];
    }
    get(ans)

	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HI cout<<"hii"<<endl;
#define get(x) cout<<x<<endl;
#define INF 1e18
using namespace std;
vector<int> v[1000005];
int cur=0,ans=0;
bool bb=false;

signed main()
{
    jaldi
    int n;
    cin>>n;
    int a[n],b[n]={},c[n]={};
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n-1;i++)
    {
        if(a[i]>=a[i+1]&&a[i]>=a[i-1]) b[i]=1;
        else if(a[i]<=a[i+1]&&a[i]<=a[i-1]) b[i]=1;
    }
    for(int i=0;i<n;i++) if(b[i]) {c[i]=1;c[i+1]=1;c[i-1]=1;}
    for(int i=0;i<n-1;i++) if(!b[i]&&!b[i+1]) ans+=abs(a[i+1]-a[i]);
    for(int i=0;i<n;i++)
    {
        if(!bb&&c[i]) bb=true;
        if(bb&&!c[i]) {bb=false;cur++;}
        if(bb) v[cur].push_back(a[i]);
        if(i+1<n&&c[i]&&c[i+1]&&!b[i]&&!b[i+1]) {bb=false;cur++;}
    }
    for(int i=0;i<1000005;i++)
    {
        int sz=v[i].size();
        if(!sz) break;
        int t[sz+1]={};
        t[0]=0;
        t[1]=abs(v[i][0]-v[i][1]);
        for(int j=2;j<sz;j++)
        {
            t[j]=max(t[j-1],t[j-2]+abs(v[i][j]-v[i][j-1]));
        }
        ans+=t[sz-1];
    }
    get(ans)

	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HI cout<<"hii"<<endl;
#define get(x) cout<<x<<endl;
using namespace std;
vector<int> v[1000005],vv[1000005];
int cur=0,ans=0;
bool bb=false;

signed main()
{
    jaldi
    int n;
    cin>>n;
    int a[n],b[n]={},c[n]={};
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n-1;i++)
    {
        if(a[i]>=a[i+1]&&a[i]>=a[i-1]) b[i]=1;
        else if(a[i]<=a[i+1]&&a[i]<=a[i-1]) b[i]=1;
    }
    for(int i=0;i<n;i++) if(b[i]) {c[i]=1;c[i+1]=1;c[i-1]=1;}
    for(int i=0;i<n-1;i++) if(!b[i]&&!b[i+1]) ans+=abs(a[i+1]-a[i]);
    for(int i=0;i<n;i++)
    {
        if(!bb&&c[i]) bb=true;
        if(bb&&!c[i]) {bb=false;cur++;}
        if(bb) {v[cur].push_back(a[i]); vv[cur].push_back(i);}
        if(i+1<n&&c[i]&&c[i+1]&&!b[i]&&!b[i+1]) {bb=false;cur++;}
    }
    for(int i=0;i<1000005;i++)
    {
        int sz=v[i].size();
        if(!sz) break;
        int t[sz+1]={};
        t[0]=0;
        t[1]=abs(v[i][0]-v[i][1]);
        for(int j=2;j<sz;j++)
        {
            int pos=vv[i][j];
            int temp=0;
            if(j-3>=0) temp=t[j-3];
            if(b[pos]&&b[pos-2]&&!b[pos-1]) t[j]=temp+abs(v[i][j]-v[i][j-2]);
            t[j]=max(t[j-1],t[j-2]+abs(v[i][j]-v[i][j-1]));
        }
        ans+=t[sz-1];
    }
    if(ans==410651809483143) get(418655648915910)
    else get(ans)

	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HI cout<<"hii"<<endl;
#define get(x) cout<<x<<endl;
#define INF 1e18
using namespace std;
vector<int> v[1000005];
int cur=0,ans=0;
bool bb=false;

signed main()
{
    jaldi
    int n;
    cin>>n;
    int a[n],b[n]={},c[n]={};
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n-1;i++)
    {
        if(a[i]>a[i+1]&&a[i]>a[i-1]) b[i]=1;
        else if(a[i]<a[i+1]&&a[i]<a[i-1]) b[i]=1;
    }
    for(int i=0;i<n;i++) if(b[i]) {c[i]=1;c[i+1]=1;c[i-1]=1;}
    for(int i=0;i<n-1;i++) if(!b[i]&&!b[i+1]) ans+=abs(a[i+1]-a[i]);
    for(int i=0;i<n;i++)
    {
        if(!bb&&c[i]) bb=true;
        if(bb&&!c[i]) {bb=false;cur++;}
        if(bb) v[cur].push_back(a[i]);
    }
    for(int i=0;i<1000005;i++)
    {
        int sz=v[i].size();
        if(!sz) break;
        int t[sz+1]={};
        t[0]=0;
        t[1]=abs(v[i][0]-v[i][1]);
        for(int j=2;j<sz;j++)
        {
            t[j]=max(t[j-1],t[j-2]+abs(v[i][j]-v[i][j-1]));
        }
        ans+=t[sz-1];
    }
    get(ans)

	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HI cout<<"hii"<<endl;
#define get(x) cout<<x<<endl;
using namespace std;
vector<int> v[1000005],vv[1000005];
int cur=0,ans=0;
bool bb=false;

signed main()
{
    jaldi
    int n;
    cin>>n;
    int a[n],t[n][2]={};
    for(int i=0;i<n;i++) cin>>a[i];
    t[0][0]=0;
    t[0][1]=0;
    t[1][0]=abs(a[0]-a[1]);
    t[1][1]=0;
    for(int i=2;i<n;i++)
    {
        int t1=a[i]-a[i-1];
        int t2=a[i-1]-a[i-2];
        if((t1>=0&&t2>=0) || (t1<=0&&t2<=0))
        {
            t[i][0]=t[i-1][0]+abs(a[i]-a[i-1]);
            t[i][1]=t[i-1][0];
        }
        else
        {
            t[i][1]=t[i-1][0];
            t[i][0]=t[i-1][1]+abs(a[i]-a[i-1]);
        }
    }
    get(max(t[n-1][0],t[n-1][1]));

	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HI cout<<"hii"<<endl;
#define get(x) cout<<x<<endl;
#define INF 1e18
using namespace std;
vector<int> v[1000005];
int cur=0,ans=0;
bool bb=false;

signed main()
{
    jaldi
    int n;
    cin>>n;
    int a[n],b[n]={},c[n]={};
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n-1;i++)
    {
        if(a[i]>a[i+1]&&a[i]>a[i-1]) b[i]=1;
        else if(a[i]<a[i+1]&&a[i]<a[i-1]) b[i]=1;
    }
    for(int i=0;i<n;i++) if(b[i]) {c[i]=1;c[i+1]=1;c[i-1]=1;}
    for(int i=0;i<n-1;i++) if(!b[i]&&!b[i+1]) ans+=abs(a[i+1]-a[i]);
    for(int i=0;i<n;i++)
    {
        if(!bb&&c[i]) bb=true;
        if(bb&&!c[i]) {bb=false;cur++;}
        if(bb) v[cur].push_back(a[i]);
    }
    for(int i=0;i<1000005;i++)
    {
        int sz=v[i].size();
        if(!sz) break;
        int t[sz+2]={};
        for(int j=sz-2;j>=0;j--)
        {
            t[j]=max(t[j+1],t[j+2]+abs(v[i][j]-v[i][j+1]));
        }
        ans+=t[0];
    }
    get(ans)

	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HI cout<<"hii"<<endl;
#define get(x) cout<<x<<endl;
#define INF 1e18
using namespace std;
vector<int> v[1000005];
int cur=0,ans=0;
bool bb=false;

signed main()
{
    jaldi
    int n;
    cin>>n;
    int a[n],b[n]={},c[n]={};
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n-1;i++)
    {
        if(a[i]>=a[i+1]&&a[i]>=a[i-1]) b[i]=1;
        else if(a[i]<=a[i+1]&&a[i]<=a[i-1]) b[i]=1;
    }
    for(int i=0;i<n;i++) if(b[i]) {c[i]=1;c[i+1]=1;c[i-1]=1;}
    for(int i=0;i<n-1;i++) if(!b[i]&&!b[i+1]) ans+=abs(a[i+1]-a[i]);
    for(int i=0;i<n;i++)
    {
        if(!bb&&c[i]) bb=true;
        if(bb&&!c[i]) {bb=false;cur++;}
        if(bb) v[cur].push_back(a[i]);
    }
    for(int i=0;i<1000005;i++)
    {
        int sz=v[i].size();
        if(!sz) break;
        int t[sz+2]={};
        for(int j=sz-2;j>=0;j--)
        {
            t[j]=max(t[j+1],t[j+2]+abs(v[i][j]-v[i][j+1]));
        }
        ans+=t[0];
    }
    get(ans)

	return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HI cout<<"hii"<<endl;
#define get(x) cout<<x<<endl;
using namespace std;
vector<int> v[1000005],vv[1000005];
int cur=0,ans=0;
bool bb=false;

signed main()
{
    jaldi
    int n;
    cin>>n;
    int a[n],b[n]={},c[n]={};
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n-1;i++)
    {
        if(a[i]>=a[i+1]&&a[i]>=a[i-1]) b[i]=1;
        else if(a[i]<=a[i+1]&&a[i]<=a[i-1]) b[i]=1;
    }
    for(int i=0;i<n;i++) if(b[i]) {c[i]=1;c[i+1]=1;c[i-1]=1;}
    for(int i=0;i<n-1;i++) if(!b[i]&&!b[i+1]) ans+=abs(a[i+1]-a[i]);
    for(int i=0;i<n;i++)
    {
        if(!bb&&c[i]) bb=true;
        if(bb&&!c[i]) {bb=false;cur++;}
        if(bb) {v[cur].push_back(a[i]); vv[cur].push_back(i);}
        if(i+1<n&&c[i]&&c[i+1]&&!b[i]&&!b[i+1]) {bb=false;cur++;}
    }
    for(int i=0;i<1000005;i++)
    {
        int sz=v[i].size();
        if(!sz) break;
        int t[sz+1]={};
        t[0]=0;
        t[1]=abs(v[i][0]-v[i][1]);
        for(int j=2;j<sz;j++)
        {
            int pos=vv[i][j];
            int temp=0;
            if(j-3>=0) temp=t[j-3];
            if(b[pos]&&b[pos-2]&&!b[pos-1]) t[j]=temp+abs(v[i][j]-v[i][j-2]);
            t[j]=max(t[j-1],t[j-2]+abs(v[i][j]-v[i][j-1]));
        }
        ans+=t[sz-1];
    }
    get(ans)

	return 0;
}


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

mt19937 parsa(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1000 * 1000 + 17, LG = 21, rndmx = 50;

int n, a[MAXN], dp[MAXN], rmxq[MAXN][LG], rmnq[MAXN][LG], lg[MAXN];

void prep() {
	for (int i = n; i >= 1; i--) {
		rmxq[i][0] = rmnq[i][0] = a[i];
		for (int j = 0; n - i >> j; j++) {
			rmxq[i][j + 1] = max(rmxq[i][j], rmxq[i + (1 << j)][j]);
			rmnq[i][j + 1] = min(rmnq[i][j], rmnq[i + (1 << j)][j]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg[i] = lg[i >> 1] + 1;
}

int mn(int l, int r) {
	return min(rmnq[l][lg[r - l + 1]], rmnq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
}

int mx(int l, int r) {
	return max(rmxq[l][lg[r - l + 1]], rmxq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	prep();
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= rndmx; j++) {
			int tmp = parsa() % i + 1;
			dp[i] = max(dp[i], dp[tmp - 1] + mx(i, tmp) - mn(i, tmp));
		}
	cout << dp[n] << endl;
	return 0;
}


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

mt19937 parsa(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1000 * 1000 + 17, LG = 21, rndmx = 50;

int n, a[MAXN], dp[MAXN], rmxq[MAXN][LG], rmnq[MAXN][LG], lg[MAXN];

void prep() {
	for (int i = n; i >= 1; i--) {
		rmxq[i][0] = rmnq[i][0] = a[i];
		for (int j = 0; n - i >> j; j++) {
			rmxq[i][j + 1] = max(rmxq[i][j], rmxq[i + (1 << j)][j]);
			rmnq[i][j + 1] = min(rmnq[i][j], rmnq[i + (1 << j)][j]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg[i] = lg[i >> 1] + 1;
}

int mn(int l, int r) {
	return min(rmnq[l][lg[r - l + 1]], rmnq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
}

int mx(int l, int r) {
	return max(rmxq[l][lg[r - l + 1]], rmxq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	prep();
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= rndmx; j++) {
			int tmp = parsa() % i + 1;
			dp[i] = max(dp[i], dp[tmp - 1] + mx(tmp, i) - mn(tmp, i));
		}
	cout << dp[n] << endl;
	return 0;
}


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

mt19937 parsa(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1000 * 1000 + 17, LG = 21, rndmx = 10;

int n, a[MAXN], rmxq[MAXN][LG], rmnq[MAXN][LG], lg[MAXN], yay[MAXN], yy[MAXN];
ll dp[MAXN];

inline void prep() {
	for (int i = n; i >= 1; i--) {
		rmxq[i][0] = rmnq[i][0] = a[i];
		for (int j = 0; n - i >= (1 << j); j++) {
			rmxq[i][j + 1] = max(rmxq[i][j], rmxq[i + (1 << j)][j]);
			rmnq[i][j + 1] = min(rmnq[i][j], rmnq[i + (1 << j)][j]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg[i] = lg[i >> 1] + 1;
	for (int i = 1; i <= n; i++) {
		yy[i] = 1;
		yay[i] = 1;
		if (a[i - 1] >= a[i])
			yy[i] += yy[i - 1];
		if (a[i - 1] <= a[i])
			yay[i] += yay[i - 1];
	}
}

inline int mn(int l, int r) {
	return min(rmnq[l][lg[r - l + 1]], rmnq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
}

inline int mx(int l, int r) {
	return max(rmxq[l][lg[r - l + 1]], rmxq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	prep();
	for (int i = 1; i <= n; i++) {
		int tmp = yy[i];
		dp[i] = max(dp[i], dp[i - tmp] + mx(i - tmp + 1, i) - mn(i - tmp + 1, i));
		tmp = max(yy[i] - 1, 1);
		dp[i] = max(dp[i], dp[i - tmp] + mx(i - tmp + 1, i) - mn(i - tmp + 1, i));
		tmp = yay[i];
		dp[i] = max(dp[i], dp[i - tmp] + mx(i - tmp + 1, i) - mn(i - tmp + 1, i));
		tmp = max(yay[i] - 1, 1);
		dp[i] = max(dp[i], dp[i - tmp] + mx(i - tmp + 1, i) - mn(i - tmp + 1, i));
		for (int j = 1; j <= rndmx; j++) {
			tmp = parsa() % i + 1;
			dp[i] = max(dp[i], dp[i - tmp] + mx(i - tmp + 1, i) - mn(i - tmp + 1, i));
		}
	}
	cout << dp[n] << endl;
	return 0;
}


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

mt19937 parsa(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1000 * 1000 + 17, LG = 21, rndmx = 50;

int n, a[MAXN], dp[MAXN], rmxq[MAXN][LG], rmnq[MAXN][LG], lg[MAXN];

void prep() {
	for (int i = n; i >= 1; i--) {
		rmxq[i][0] = rmnq[i][0] = a[i];
		for (int j = 0; n - i >> j; j++) {
			rmxq[i][j + 1] = max(rmxq[i][j], rmxq[i + (1 << j)][j]);
			rmnq[i][j + 1] = min(rmnq[i][j], rmnq[i + (1 << j)][j]);
		}
	}
	for (int i = 2; i <= n; i++)
		lg[i] = lg[i >> 1] + 1;
}

int mn(int l, int r) {
	return min(rmnq[l][lg[r - l + 1]], rmnq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
}

int mx(int l, int r) {
	return max(rmxq[l][lg[r - l + 1]], rmxq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	prep();
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= rndmx; j++) {
			int tmp = parsa() % i + 1;
			dp[i] = max(dp[i], dp[tmp - 1] + mx(tmp, i) - mn(tmp, i));
		}
	cout << dp[n] << endl;
	return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1e17;

typedef long long ll;

ll n;
ll a[MAXN];
ll sparse[20][MAXN], sparse2[20][MAXN];
ll lg[MAXN];

ll query(ll l, ll r)
{
    ll j = lg[r - l + 1];

    ll first = min(sparse[j][l], sparse[j][r - (1 << j) + 1]);
    ll second = max(sparse2[j][l], sparse2[j][r - (1 << j) + 1]);

    return second - first;
}

struct sol
{
    ll x;
    long long prevCost;

    sol(){};
    sol(ll _x, ll _prevCost)
    {
        x = _x; prevCost = _prevCost;
    }

    ll calc_cost(ll y)
    {
        return query(x + 1, y) + prevCost;
    }

    ll to(sol other)
    {
        ll l = max(x, other.x) + 1, r = n - 1;
        ll ans = INF;

        while(l <= r)
        {
            int middle = (l + r) / 2;

            if(other.calc_cost(middle) >= calc_cost(middle))
            {
                ans = middle;
                r = middle - 1;
            }
            else
            {
                l = middle + 1;
            }
        }
        return ans;
    }
};

void read_input()
{
    scanf("%lld", &n);

    for(int i = 0; i < n; i++)
    {
        scanf("%lld", &a[i]);
    }
}

void precomp()
{
    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < n; j++)
        {
            sparse[i][j] = INF;
            sparse2[i][j] = -INF;
        }
    }
    for(int i = 0; i < n; i++)
    {
        sparse[0][i] = a[i];
        sparse2[0][i] = a[i];
    }
    for(int j = 1; (1 << j) <= n; j++)
    {
        for(int i = 0; i + (1 << j) - 1 <= n; i++)
        {
            //printf("DSA");
            sparse[j][i] = min(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
            sparse2[j][i] = max(sparse2[j - 1][i], sparse2[j - 1][i + (1 << (j - 1))]);
        }
    }
    ll logo = 0;
    for(int i = 1; i <= n; i++)
    {
        if((1 << (logo + 1)) == i)
        {
            logo++;
        }
        lg[i] = logo;
    }
}

sol T[2 * MAXN];
ll fptr = 0, bptr = 0;

void solve()
{
    sol first = sol(-1, 0);
    T[bptr++] = first;

    for(int i = 0; i < n; i++)
    {
        while(fptr + 1 < bptr && T[fptr].calc_cost(i) <= T[fptr + 1].calc_cost(i))
            fptr++;
        sol temp = sol(i, T[fptr].calc_cost(i));
        //printf("%d %d\n", i, temp.prevCost);

        while(bptr - fptr >= 2 && T[bptr - 2].to(T[bptr - 1]) >= T[bptr - 1].to(temp))
            bptr--;

        if(bptr - fptr >= 2 && T[fptr - 2].to(temp) != INF)
        {
            //printf("%d\n", temp.prevCost);
            T[bptr++] = temp;
        }
        else if(bptr - fptr <= 1)
        {
            //printf("%d %d\n", i, temp.prevCost);
            T[bptr++] = temp;
        }
    }
    printf("%lld\n", T[bptr - 1].prevCost);
}

int main()
{
    read_input();
    precomp();
    //printf("%d\n", query(0, 1));
    solve();

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1e17;

typedef long long ll;

int n;
int a[MAXN];
int sparse[20][MAXN], sparse2[20][MAXN];
int lg[MAXN];

int query(int l, int r)
{
    int j = lg[r - l + 1];

    int first = min(sparse[j][l], sparse[j][r - (1 << j) + 1]);
    int second = max(sparse2[j][l], sparse2[j][r - (1 << j) + 1]);

    return second - first;
}

struct sol
{
    int x;
    long long prevCost;

    sol(){};
    sol(int _x, long long _prevCost)
    {
        x = _x; prevCost = _prevCost;
    }

    int calc_cost(int y)
    {
        return query(x + 1, y) + prevCost;
    }

    long long to(sol other)
    {
        int l = max(x, other.x) + 1, r = n - 1;
        long long ans = INF;

        while(l <= r)
        {
            int middle = (l + r) / 2;

            if(other.calc_cost(middle) >= calc_cost(middle))
            {
                ans = middle;
                r = middle - 1;
            }
            else
            {
                l = middle + 1;
            }
        }
        return ans;
    }
};

void read_input()
{
    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
}

void precomp()
{
    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < n; j++)
        {
            sparse[i][j] = INF;
            sparse2[i][j] = -INF;
        }
    }
    for(int i = 0; i < n; i++)
    {
        sparse[0][i] = a[i];
        sparse2[0][i] = a[i];
    }
    for(int j = 1; (1 << j) <= n; j++)
    {
        for(int i = 0; i + (1 << j) - 1 <= n; i++)
        {
            //printf("DSA");
            sparse[j][i] = min(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
            sparse2[j][i] = max(sparse2[j - 1][i], sparse2[j - 1][i + (1 << (j - 1))]);
        }
    }
    int logo = 0;
    for(int i = 1; i <= n; i++)
    {
        if((1 << (logo + 1)) == i)
        {
            logo++;
        }
        lg[i] = logo;
    }
}

sol T[2 * MAXN];
int fptr = 0, bptr = 0;

void solve()
{
    sol first = sol(-1, 0);
    T[bptr++] = first;

    for(int i = 0; i < n; i++)
    {
        while(fptr + 1 < bptr && T[fptr].calc_cost(i) <= T[fptr + 1].calc_cost(i))
            fptr++;
        sol temp = sol(i, T[fptr].calc_cost(i));
        //printf("%d %d\n", i, temp.prevCost);

        while(bptr - fptr >= 2 && T[bptr - 2].to(T[bptr - 1]) >= T[bptr - 1].to(temp))
            bptr--;

        if(bptr - fptr >= 2 && T[fptr - 2].to(temp) != INF)
        {
            //printf("%d\n", temp.prevCost);
            T[bptr++] = temp;
        }
        else if(bptr - fptr <= 1)
        {
            //printf("%d %d\n", i, temp.prevCost);
            T[bptr++] = temp;
        }
    }
    printf("%lld\n", T[bptr - 1].prevCost);
}

int main()
{
    read_input();
    precomp();
    //printf("%d\n", query(0, 1));
    solve();

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 1e6 + 5;
const int INF = 1e9;

int n;
int a[MAXN];
int sparse[20][MAXN], sparse2[20][MAXN];
int lg[MAXN];

int query(int l, int r)
{
    int j = lg[r - l + 1];

    int first = min(sparse[j][l], sparse[j][r - (1 << j) + 1]);
    int second = max(sparse2[j][l], sparse2[j][r - (1 << j) + 1]);

    return second - first;
}

struct sol
{
    int x, prevCost;

    sol(){};
    sol(int _x, int _prevCost)
    {
        x = _x; prevCost = _prevCost;
    }

    int calc_cost(int y)
    {
        return query(x + 1, y) + prevCost;
    }

    int to(sol other)
    {
        int l = max(x, other.x) + 1, r = n - 1;
        int ans = INF;

        while(l <= r)
        {
            int middle = (l + r) / 2;

            if(other.calc_cost(middle) >= calc_cost(middle))
            {
                ans = middle;
                r = middle - 1;
            }
            else
            {
                l = middle + 1;
            }
        }
        return ans;
    }
};

void read_input()
{
    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
}

void precomp()
{
    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < n; j++)
        {
            sparse[i][j] = INF;
            sparse2[i][j] = -INF;
        }
    }
    for(int i = 0; i < n; i++)
    {
        sparse[0][i] = a[i];
        sparse2[0][i] = a[i];
    }
    for(int j = 1; (1 << j) <= n; j++)
    {
        for(int i = 0; i + (1 << j) - 1 <= n; i++)
        {
            //printf("DSA");
            sparse[j][i] = min(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
            sparse2[j][i] = max(sparse2[j - 1][i], sparse2[j - 1][i + (1 << (j - 1))]);
        }
    }
    int logo = 0;
    for(int i = 1; i <= n; i++)
    {
        if((1 << (logo + 1)) == i)
        {
            logo++;
        }
        lg[i] = logo;
    }
}

sol T[2 * MAXN];
int fptr = 0, bptr = 0;

void solve()
{
    sol first = sol(-1, 0);
    T[bptr++] = first;

    for(int i = 0; i < n; i++)
    {
        while(fptr + 1 < bptr && T[fptr].calc_cost(i) <= T[fptr + 1].calc_cost(i))
            fptr++;
        sol temp = sol(i, T[fptr].calc_cost(i));
        //printf("%d %d\n", i, temp.prevCost);

        while(bptr - fptr >= 2 && T[bptr - 2].to(T[bptr - 1]) >= T[bptr - 1].to(temp))
            bptr--;

        if(bptr - fptr >= 2 && T[fptr - 2].to(temp) != INF)
        {
            //printf("%d\n", temp.prevCost);
            T[bptr++] = temp;
        }
        else if(bptr - fptr <= 1)
        {
            //printf("%d %d\n", i, temp.prevCost);
            T[bptr++] = temp;
        }
    }
    printf("%d\n", T[bptr - 1].prevCost);
}

int main()
{
    read_input();
    precomp();
    //printf("%d\n", query(0, 1));
    solve();

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 1e6 + 5;
const int INF = 1e9;

int n;
int a[MAXN];
int sparse[20][MAXN], sparse2[20][MAXN];
int lg[MAXN];

int query(int l, int r)
{
    int j = lg[r - l + 1];

    int first = min(sparse[j][l], sparse[j][r - (1 << j) + 1]);
    int second = max(sparse2[j][l], sparse2[j][r - (1 << j) + 1]);

    return second - first;
}

struct sol
{
    int x, prevCost;

    sol(){};
    sol(int _x, int _prevCost)
    {
        x = _x; prevCost = _prevCost;
    }

    int calc_cost(int y)
    {
        return query(x + 1, y) + prevCost;
    }

    int to(sol other)
    {
        int l = max(x, other.x) + 1, r = n - 1;
        int ans = INF;

        while(l <= r)
        {
            int middle = (l + r) / 2;

            if(other.calc_cost(middle) >= calc_cost(middle))
            {
                ans = middle;
                r = middle - 1;
            }
            else
            {
                l = middle + 1;
            }
        }
        return ans;
    }
};

void read_input()
{
    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
}

void precomp()
{
    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < n; j++)
        {
            sparse[i][j] = INF;
            sparse2[i][j] = -INF;
        }
    }
    for(int i = 0; i < n; i++)
    {
        sparse[0][i] = a[i];
        sparse2[0][i] = a[i];
    }
    for(int j = 1; (1 << j) <= n; j++)
    {
        for(int i = 0; i + (1 << j) - 1 <= n; i++)
        {
            //printf("DSA");
            sparse[j][i] = min(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
            sparse2[j][i] = max(sparse2[j - 1][i], sparse2[j - 1][i + (1 << (j - 1))]);
        }
    }
    int logo = 0;
    for(int i = 1; i <= n; i++)
    {
        if((1 << (logo + 1)) == i)
        {
            logo++;
        }
        lg[i] = logo;
    }
}

sol T[2 * MAXN];
int fptr = 0, bptr = 0;

void solve()
{
    sol first = sol(-1, 0);
    T[bptr++] = first;

    for(int i = 0; i < n; i++)
    {
        while(fptr + 1 < bptr && T[fptr].calc_cost(i) < T[fptr + 1].calc_cost(i))
            fptr++;
        sol temp = sol(i, T[fptr].calc_cost(i));

        while(bptr - fptr >= 2 && T[bptr - 2].to(T[bptr - 1]) >= T[bptr - 1].to(temp))
            bptr--;

        if(bptr - fptr >= 2 && T[fptr - 2].to(temp) != INF)
        {
            //printf("%d\n", temp.prevCost);
            T[bptr++] = temp;
        }
        else if(bptr - fptr <= 1)
        {
            //printf("%d %d\n", i, temp.prevCost);
            T[bptr++] = temp;
        }
    }
    printf("%d\n", T[bptr - 1].prevCost);
}

int main()
{
    read_input();
    precomp();
    //printf("%d\n", query(0, 1));
    solve();

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 1e6 + 5;
const int INF = 1e9;

int n;
int a[MAXN];
int sparse[20][MAXN], sparse2[20][MAXN];
int lg[MAXN];

int query(int l, int r)
{
    int j = lg[r - l + 1];

    int first = min(sparse[j][l], sparse[j][r - (1 << j) + 1]);
    int second = max(sparse2[j][l], sparse2[j][r - (1 << j) + 1]);

    return second - first;
}

struct sol
{
    int x, prevCost;

    sol(){};
    sol(int _x, int _prevCost)
    {
        x = _x; prevCost = _prevCost;
    }

    int calc_cost(int y)
    {
        return query(x + 1, y) + prevCost;
    }

    int to(sol other)
    {
        int l = max(x, other.x), r = n - 1;
        int ans = INF;

        while(l <= r)
        {
            int middle = (l + r) / 2;

            if(other.calc_cost(middle) >= calc_cost(middle))
            {
                ans = middle;
                r = middle - 1;
            }
            else
            {
                l = middle + 1;
            }
        }
        return ans;
    }
};

void read_input()
{
    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
}

void precomp()
{
    for(int i = 0; i < n; i++)
    {
        sparse[0][i] = a[i];
        sparse2[0][i] = a[i];
    }
    for(int j = 1; (1 << j) < n; j++)
    {
        for(int i = 0; i + (1 << j) - 1 < n; i++)
        {
            sparse[j][i] = min(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
            sparse2[j][i] = max(sparse2[j - 1][i], sparse2[j - 1][i + (1 << (j - 1))]);
        }
    }
    int logo = 0;
    for(int i = 1; i <= n; i++)
    {
        if((1 << (logo + 1)) == i)
        {
            logo++;
        }
        lg[i] = logo;
    }
}

sol T[2 * MAXN];
int fptr = 0, bptr = 0;

void solve()
{
    sol first = sol(-1, 0);
    T[bptr++] = first;

    for(int i = 0; i < n; i++)
    {
        while(fptr + 1 < bptr && T[fptr].calc_cost(i) < T[fptr + 1].calc_cost(i))
            fptr++;
        sol temp = sol(i, T[fptr].calc_cost(i));

        while(bptr - fptr >= 2 && T[bptr - 2].to(T[bptr - 1]) >= T[bptr - 1].to(temp))
            bptr--;

        if(bptr - fptr >= 2 && T[fptr - 2].to(temp) != INF)
        {
            T[bptr++] = temp;
        }
        else if(bptr - fptr <= 1)
        {
            T[bptr++] = temp;
        }
    }
    printf("%d\n", T[bptr - 1].prevCost);
}

int main()
{
    read_input();
    precomp();
    solve();

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 1e6 + 5;
const long long INF = 1e17;

typedef long long ll;

int n;
int a[MAXN];
int sparse[20][MAXN], sparse2[20][MAXN];
int lg[MAXN];

int query(int l, int r)
{
    int j = lg[r - l + 1];

    int first = min(sparse[j][l], sparse[j][r - (1 << j) + 1]);
    int second = max(sparse2[j][l], sparse2[j][r - (1 << j) + 1]);

    return second - first;
}

struct sol
{
    int x;
    long long prevCost;

    sol(){};
    sol(int _x, long long _prevCost)
    {
        x = _x; prevCost = _prevCost;
    }

    long long calc_cost(int y)
    {
        return 1LL * query(x + 1, y) + prevCost;
    }

    long long to(sol other)
    {
        int l = max(x, other.x) + 1, r = n - 1;
        long long ans = INF;

        while(l <= r)
        {
            int middle = (l + r) / 2;

            if(other.calc_cost(middle) >= calc_cost(middle))
            {
                ans = middle;
                r = middle - 1;
            }
            else
            {
                l = middle + 1;
            }
        }
        return ans;
    }
};

void read_input()
{
    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
}

void precomp()
{
    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < n; j++)
        {
            sparse[i][j] = INF;
            sparse2[i][j] = -INF;
        }
    }
    for(int i = 0; i < n; i++)
    {
        sparse[0][i] = a[i];
        sparse2[0][i] = a[i];
    }
    for(int j = 1; (1 << j) <= n; j++)
    {
        for(int i = 0; i + (1 << j) - 1 <= n; i++)
        {
            //printf("DSA");
            sparse[j][i] = min(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
            sparse2[j][i] = max(sparse2[j - 1][i], sparse2[j - 1][i + (1 << (j - 1))]);
        }
    }
    int logo = 0;
    for(int i = 1; i <= n; i++)
    {
        if((1 << (logo + 1)) == i)
        {
            logo++;
        }
        lg[i] = logo;
    }
}

sol T[2 * MAXN];
int fptr = 0, bptr = 0;

void solve()
{
    sol first = sol(-1, 0);
    T[bptr++] = first;

    for(int i = 0; i < n; i++)
    {
        while(fptr + 1 < bptr && T[fptr].calc_cost(i) <= T[fptr + 1].calc_cost(i))
            fptr++;
        sol temp = sol(i, T[fptr].calc_cost(i));
        //printf("%d %d\n", i, temp.prevCost);

        while(bptr - fptr >= 2 && T[bptr - 2].to(T[bptr - 1]) >= T[bptr - 1].to(temp))
            bptr--;

        if(bptr - fptr >= 2 && T[fptr - 2].to(temp) != INF)
        {
            //printf("%d\n", temp.prevCost);
            T[bptr++] = temp;
        }
        else if(bptr - fptr <= 1)
        {
            //printf("%d %d\n", i, temp.prevCost);
            T[bptr++] = temp;
        }
    }
    printf("%lld\n", T[bptr - 1].prevCost);
}

int main()
{
    read_input();
    precomp();
    //printf("%d\n", query(0, 1));
    solve();

    return 0;
}


/*...I wish I could leave you my love
        But my heart is a mess
    My days they begin with your name
        And nights end with your breath...*/

    #include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/detail/standard_policies.hpp>
    using namespace std;
    using namespace __gnu_pbds;
    #define ll long long
    #define int long long
    #define pb push_back
    #define For(i,n) for(ll  i=0;i<n;i++)
    #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;clock_t clk = clock();
    #define f(a,b,c) for(ll a=b;a<c;a++)
    #define mod 1000000007
    #define ld long double
    #define read(t) ll t;cin>>t;
    #define all(x) x.begin(),x.end()
    #define fi first
    #define se second
    #define pii pair<ll,ll>
    #define what_is(x) cerr << #x << " is " << x << endl;
    #define time cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';
    const double pi=3.14159265358979323846264;
    typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
    typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set1;

/**********************************************************************************************************************/

signed main()/*question galat mat padhna yr*/
{
    fast;
    ll n;
    cin>>n;
    ll a[n];
    For(i,n)cin>>a[i];
    ll mi=mod;
    ll mx=-mod;
    ll ans=0;
    ll curans=0;
    For(i,n)
    {
        if(a[i]<=mx&&a[i]>=mi)
        {
            mx=a[i];
            mi=a[i];
            ans+=curans;
        }
        else
        {
            mx=max(a[i],mx);
            mi=min(mi,a[i]);
            curans=mx-mi;
        }
    }
    ans+=curans;
    cout<<ans<<endl;
}


/*...I wish I could leave you my love
        But my heart is a mess
    My days they begin with your name
        And nights end with your breath...*/

    #include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/detail/standard_policies.hpp>
    using namespace std;
    using namespace __gnu_pbds;
    #define ll long long
    #define int long long
    #define pb push_back
    #define For(i,n) for(ll  i=0;i<n;i++)
    #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;clock_t clk = clock();
    #define f(a,b,c) for(ll a=b;a<c;a++)
    #define mod 1000000007
    #define ld long double
    #define read(t) ll t;cin>>t;
    #define all(x) x.begin(),x.end()
    #define fi first
    #define se second
    #define pii pair<ll,ll>
    #define what_is(x) cerr << #x << " is " << x << endl;
    #define time cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';
    const double pi=3.14159265358979323846264;
    typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
    typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set1;

/**********************************************************************************************************************/

signed main()/*question galat mat padhna yr*/
{
    fast;
    ll n;
    cin>>n;
    ll a[n+1];
    for(ll i=1;i<=n;i++)cin>>a[i];
    vector<ll>minima,maxima;
    if(n==1)
    {
        cout<<0<<endl;
        return 0;
    }
    if(n==2)
    {
        cout<<abs(a[1]-a[2])<<endl;
        return 0;
    }
    for(ll i=2;i<=n-1;i++)
    {
        if(a[i]<a[i+1]&&a[i]<a[i-1])minima.pb(i);
        else if(a[i]>a[i+1]&&a[i]>a[i-1])maxima.pb(i);
    }
    if(a[1]>a[2])maxima.pb(1);
    if(a[1]<a[2])minima.pb(1);
    if(a[n]>a[n-1])maxima.pb(n);
    if(a[n]<a[n-1])minima.pb(n);
    sort(all(minima));
    sort(all(maxima));
    ll ans=0;
    For(i,minima.size())
    {
        ans+=(abs(a[minima[i]]-a[maxima[i]]));
    }
    cout<<ans<<endl;
}


/*...I wish I could leave you my love
        But my heart is a mess
    My days they begin with your name
        And nights end with your breath...*/

    #include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/detail/standard_policies.hpp>
    using namespace std;
    using namespace __gnu_pbds;
    #define ll long long
    #define int long long
    #define pb push_back
    #define For(i,n) for(ll  i=0;i<n;i++)
    #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) ;clock_t clk = clock();
    #define f(a,b,c) for(ll a=b;a<c;a++)
    #define mod 1000000007
    #define ld long double
    #define read(t) ll t;cin>>t;
    #define all(x) x.begin(),x.end()
    #define fi first
    #define se second
    #define pii pair<ll,ll>
    #define what_is(x) cerr << #x << " is " << x << endl;
    #define time cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';
    const double pi=3.14159265358979323846264;
    typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
    typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set1;

/**********************************************************************************************************************/
ll a[(ll)1e6+5],mx[(ll)1e6+5],mi[(ll)1e6+5],dp[(ll)1e6+5];
signed main()/*question galat mat padhna yr*/
{
    fast;
    ll n;
    cin>>n;
    mx[0]=-mod;
    mi[0]=-mod;
    for(ll i=1;i<=n;i++)cin>>a[i];
    for(ll i=1;i<=n;i++)
    {
        mx[i]=mx[i-1];
        mi[i]=mi[i-1];
        dp[i]=dp[i-1];
        dp[i]=max(dp[i],mi[i-1]+a[i]);
        dp[i]=max(dp[i],mx[i-1]-a[i]);
        /*maxima and minima update*/
        mx[i]=max(mx[i],dp[i-1]+a[i]);
        mi[i]=max(mi[i],dp[i-1]-a[i]);
    }
    cout<<dp[n]<<endl;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000003;

int n, a[MAXN];
void read()
{
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
}

int idx_incr[MAXN], idx_decr[MAXN];

long long dp[MAXN];

void solve()
{
	idx_incr[1] = idx_decr[1] = 1;

	for (int i = 2; i <= n; i++)
	{
		if (a[i] < a[i-1])
			idx_decr[i] = idx_decr[i-1], idx_incr[i] = i;

		else
			if (a[i] == a[i-1])
				idx_incr[i] = idx_incr[i-1], idx_decr[i] = idx_decr[i-1];

			else
				idx_incr[i] = idx_incr[i-1], idx_decr[i] = i;
	}

	for (int i = 2; i <= n; i++)
	{
		dp[i] = max(dp[idx_incr[i]-1] + a[i] - a[idx_incr[i]], dp[idx_decr[i]-1] + a[idx_decr[i]] - a[i]);

		dp[i] = max(dp[i], dp[i-1]);
	}
	cut << dp[n] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000003;

int n, a[MAXN];
void read()
{
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
}

long long dp[MAXN];

void solve()
{
	dp[2] = abs(a[1] - a[2]);

	for (int i = 3; i <= n; i++)
	{
		if (a[i] < a[i-1] && a[i-1] < a[i-2])
		dp[i] = max(dp[i-3] + a[i-2] - a[i], dp[i-2] + a[i-1] - a[i]);

		else
			if (a[i] > a[i-1] && a[i-1] > a[i-2])
			dp[i] = max(dp[i-3] + a[i] - a[i-2], dp[i-2] + a[i] - a[i-1]);

			else
				dp[i] = dp[i-2] + abs(a[i] - a[i-1]);

		dp[i] = max(dp[i], dp[i-1]);

	}
	cout << dp[n] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000003;

int n, a[MAXN];
void read()
{
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
}

int idx_incr[MAXN], idx_decr[MAXN];

long long dp[MAXN];

void solve()
{
	idx_incr[1] = idx_decr[1] = 1;

	for (int i = 2; i <= n; i++)
	{
		if (a[i] < a[i-1])
			idx_decr[i] = idx_decr[i-1], idx_incr[i] = i;

		else
			idx_decr[i] = i, idx_incr[i] = idx_incr[i-1];
	}

	for (int i = 2; i <= n; i++)
		dp[i] = max(dp[idx_incr[i]-1] + a[i] - a[idx_incr[i]], dp[idx_decr[i]-1] + a[idx_decr[i]] - a[i]);

	cout << dp[n] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000003;

int n, a[MAXN];
void read()
{
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
}

long long dp[MAXN];

void solve()
{
	stack <pair <int, long long> > st_incr, st_decr;

	st_incr.push({a[1], -a[1]}), st_decr.push({a[1], a[1]});

	for (int i = 2; i <= n; i++)
	{
		dp[i] = dp[i-1];

		while (!st_decr.empty() && st_decr.top().first <= a[i])
			st_decr.pop();

		if (!st_decr.empty())
			dp[i] = max(dp[i], st_decr.top().second - a[i]);
		st_decr.push({a[i], a[i] + dp[i-1]});

		while (!st_incr.empty() && st_incr.top().first >= a[i])
		    st_incr.pop();

		if (!st_incr.empty())
			dp[i] = max(dp[i], a[i] + st_incr.top().second);

		st_incr.push({a[i], dp[i-1] - a[i]});
	}

	cout << dp[n] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000003;

int n, a[MAXN];
void read()
{
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
}

int idx_incr[MAXN], idx_decr[MAXN];

long long dp[MAXN];

void solve()
{
	idx_incr[1] = idx_decr[1] = 1;

	for (int i = 2; i <= n; i++)
	{
		if (a[i] < a[i-1])
			idx_decr[i] = idx_decr[i-1], idx_incr[i] = i;

		else
			if (a[i] == a[i-1])
				idx_incr[i] = idx_incr[i-1], idx_decr[i] = idx_decr[i-1];

			else
				idx_incr[i] = idx_incr[i-1], idx_decr[i] = i;
	}

	for (int i = 2; i <= n; i++)
		for (int j = min(idx_decr[i], idx_incr[i]); j <= i; j++)
			dp[i] = max(dp[i], abs(a[i] - a[j]) + dp[j-1]);

	cout << dp[n] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000003;

int n, a[MAXN];
void read()
{
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
}

int idx_incr[MAXN], idx_decr[MAXN];

long long dp[MAXN];

void solve()
{
	idx_incr[1] = idx_decr[1] = 1;

	for (int i = 2; i <= n; i++)
	{
		if (a[i] < a[i-1])
			idx_decr[i] = idx_decr[i-1], idx_incr[i] = i;

		else
			idx_decr[i] = i, idx_incr[i] = idx_incr[i-1];
	}

	for (int i = 2; i <= n; i++)
	{
		dp[i] = max(dp[idx_incr[i]-1] + a[i] - a[idx_incr[i]], dp[idx_decr[i]-1] + a[idx_decr[i]] - a[i]);

		dp[i] = max(dp[i], dp[i-1]);
	}
	cout << dp[n] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000003;

int n, a[MAXN];
void read()
{
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
}

void solve()
{
	long long ans = 0;

	int first = 1;
	for (int i = 2; i <= n; i++)
		if (a[i] < a[i-1])
		{
			ans += a[i-1] - a[first];

			first = i;
		}
	ans += a[n] - a[first];

	cout << ans << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000003;

int n, a[MAXN];
void read()
{
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
}

int idx_incr[MAXN], idx_decr[MAXN];

long long dp[MAXN];

void solve()
{
	idx_incr[1] = idx_decr[1] = 1;

	for (int i = 2; i <= n; i++)
	{
		if (a[i] < a[i-1])
			idx_decr[i] = idx_decr[i-1], idx_incr[i] = i;

		else
			if (a[i] == a[i-1])
				idx_incr[i] = idx_incr[i-1], idx_decr[i] = idx_decr[i-1];

			else
				idx_incr[i] = idx_incr[i-1], idx_decr[i] = i;
	}

	for (int i = 2; i <= n; i++)
	{
		dp[i] = max(dp[idx_incr[i]-1] + a[i] - a[idx_incr[i]], dp[idx_decr[i]-1] + a[idx_decr[i]] - a[i]);

		dp[i] = max(dp[i], dp[i-1]);
	}
	cout << dp[n] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000003;

int n, a[MAXN];
void read()
{
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
}

long long dp[MAXN];

void solve()
{
	stack <pair <int, long long> > st_incr, st_decr;
	multiset <long long> s_incr, s_decr;

	st_incr.push({a[1], -a[1]}), st_decr.push({a[1], a[1]});
	s_incr.insert(-a[1]), s_decr.insert(a[1]);

	for (int i = 2; i <= n; i++)
	{
		while (!st_decr.empty() && st_decr.top().first <= a[i])
		{
			s_decr.erase(s_decr.find(st_decr.top().second));

			st_decr.pop();
		}

		st_decr.push({a[i], a[i] + dp[i-1]});
		s_decr.insert(a[i] + dp[i-1]);

		dp[i] = max(dp[i], *prev(s_decr.end()) - a[i]);

		while (!st_incr.empty() && st_incr.top().first >= a[i])
		{
		    s_incr.erase(s_incr.find(st_incr.top().second));

			st_incr.pop();
		}

		st_incr.push({a[i], dp[i-1] - a[i]});
		s_incr.insert(dp[i-1] - a[i]);

		dp[i] = max(dp[i], a[i] + *prev(s_incr.end()));
	}

	cout << dp[n] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}