using System; using System.Collections.Generic; using System.Linq; class Program { static string ReadLine() { return Console.ReadLine().Trim(); } static void Main() { int N = int.Parse(ReadLine()); int[] A = ReadLine().Split().Select(_ => int.Parse(_)).ToArray(); int ans = 0; Stack stack = new Stack(); for (int i = 0; i < N; i++) { int v = A[i]; if (i % 2 == 1) v *= -1; while (stack.Count > 0) { int p = stack.Peek(); if (Math.Abs(p) > Math.Abs(v)) break; int pop = stack.Pop(); ans -= pop; //if (i == 4) //{ // Console.WriteLine("pop >> " + pop); // Console.WriteLine("peek >> " + stack.Peek()); // Console.WriteLine("Math.Abs(v) >> " + Math.Abs(v)); //} } stack.Push(v); ans += v; if (stack.Count >= 2) { int pop1 = stack.Pop(); int pop2 = stack.Pop(); if ((pop1 > 0 && pop2 > 0) || (pop1 < 0 && pop2 < 0)) { stack.Push(pop2); ans -= pop1; } else { stack.Push(pop2); stack.Push(pop1); } } Console.WriteLine(ans); } //Console.WriteLine(ans); } }