using System; using System.Collections.Generic; using System.Linq; class Data { public Data(int color, int height) { Color = color; Height = height; } public int Color = 0; public int Height = 0; } 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(); stack.Push(new Data(0, (int)Math.Pow(10, 9) + 1)); for (int i = 0; i < N; i++) { int v = A[i]; while (true) { Data p = stack.Peek(); if (p.Height > v) break; Data pop = stack.Pop(); ans -= pop.Color == 1 ? pop.Height : -pop.Height; } Data p2 = stack.Peek(); if (i % 2 == 0 && p2.Color == 0) { stack.Push(new Data(1, v)); ans += v; } else if (i % 2 == 1 && p2.Color == 1) { stack.Push(new Data(0, v)); ans -= v; } Console.WriteLine(ans); } } }