using System; using System.Collections.Generic; using System.Linq; using System.Numerics; namespace yukicoder { public class Program { public static void Main() { var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(x => long.Parse(x)).ToList(); long s = 0; long v = 0; while (a.Count > 0) { while (a.Count > 0 && a[a.Count - 1] - a[a.Count - 2] >= v) { s += a[a.Count - 1] - a[a.Count - 2]; a.RemoveAt(a.Count - 1); a.RemoveAt(a.Count - 1); } if(a.Count > 0) { v = a[a.Count - 1] - a[a.Count - 2]; } while (a.Count > 0 && a[0] - a[1] >= v) { s += a[0] - a[1]; a.RemoveAt(0); a.RemoveAt(0); } if (a.Count > 0) { v = a[0] - a[1]; } } Console.WriteLine(s); } } }