Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Maximum Profit

You can obtain profits from foreign exchange margin transactions. For example, if you buy 1000 dollar at a rate of 100 yen per dollar, and sell them at a rate of 108 yen per dollar, you can obtain (108 - 100) × 1000 = 8000 yen.

Write a program which reads values of a currency $R_t$ at a certain time $t$ ($t = 0, 1, 2, ... n-1$), and reports the maximum value of $R_j - R_i$ where $j > i$ .

Input

The first line contains an integer $n$. In the following $n$ lines, $R_t$ ($t = 0, 1, 2, ... n-1$) are given in order.

Output

Print the maximum value in a line.

Constraints

$2 \leq n \leq 200,000$

$1 \leq R_t \leq 10^9$

Sample Input 1

6
5
3
1
3
4
3

Sample Output 1

3

Sample Input 2

3
4
3
2

Sample Output 2

-1

Solution in C# (judge verdict: Accepted)

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;


namespace AizuOnlineJudge
{
    public class Program
    {
        /// <summary>
        /// 実行関数
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            // Programクラスの生成
            new Program().Anser2(new ConsoleInput(Console.In, ' '));
        }

        /// <summary>
        /// 答えの実行場所
        /// </summary>
        /// <param name="Input"></param>
        public void Anser1(ConsoleInput Input)
        {
            List<int> profitInfo = ProfitInfoList();

            var maxProfit = int.MinValue;

            for (int i = 0; i < profitInfo.Count; i++)
                for (int r = i + 1; r < profitInfo.Count; r++)
                {
                    if (maxProfit < profitInfo[r] - profitInfo[i])
                        maxProfit = profitInfo[r] - profitInfo[i];
                }

            Console.WriteLine(maxProfit);
            Console.ReadKey();
        }

        /// <summary>
        /// 答えの実行場所　アイズオンラインジャッジ　正答
        /// </summary>
        /// <param name="Input"></param>
        public void Anser2(ConsoleInput Input)
        {
            List<int> profitInfo = ProfitInfoList();

            // 最大利益格納用
            var maxProfit = int.MinValue;

            // 最少時利益格納用
            var tempMinProfit = int.MaxValue;


            for (int i = 0; i < profitInfo.Count - 1; i++)
            {
                // 通常計算 現在利益と次の利益の計算を行う
                if (maxProfit < profitInfo[i + 1] - profitInfo[i])
                    maxProfit = profitInfo[i + 1] - profitInfo[i];

                // 最少時利益計算　経過した時間の中で最も最少だった利益と次の利益を計算する
                if (maxProfit < profitInfo[i + 1] - tempMinProfit)
                    maxProfit = profitInfo[i + 1] - tempMinProfit;

                // 現在利益と最少時の利益を比較する
                if (profitInfo[i] < tempMinProfit)
                    tempMinProfit = profitInfo[i];
            }

            Console.WriteLine(maxProfit);
            Console.ReadKey();
        }

        /// <summary>
        /// 利益情報を取得する
        /// </summary>
        /// <returns>利益情報を格納したInteger型のList</returns>
        public  List<int> ProfitInfoList()
        {
            // ループ数の取得
            var loop = Convert.ToInt32(Console.ReadLine());

            // 利益情報格納リスト
            var profitInfo = new List<int>();

            for (int i = 0; i < loop; i++)
            {
                profitInfo.Add(Convert.ToInt32(Console.ReadLine()));
            }

            return profitInfo;
        }
    }

    public class ConsoleInput
    {
        private readonly System.IO.TextReader Stream;
        private char Separator = ' ';
        private Queue<string> InputStream;

        public ConsoleInput(System.IO.TextReader Stream, char Separator = ' ')
        {
            this.Separator = Separator;
            this.Stream = Stream;
            InputStream = new Queue<string>();
        }

        public string Read
        {
            get
            {
                if (InputStream.Count != 0)
                {
                    return InputStream.Dequeue();
                }

                string[] splitStrs = Stream.ReadLine().Split(Separator);

                for (int i = 0; i < splitStrs.Length; ++i)
                {
                    InputStream.Enqueue(splitStrs[i]);
                }

                return InputStream.Dequeue();
            }
        }

        public string ReadLine { get { return Stream.ReadLine(); } }
        public int ReadInt { get { return int.Parse(Read); } }
        public long ReadLong { get { return long.Parse(Read); } }
        public double ReadDouble { get { return double.Parse(Read); } }

        public string[] ReadStrArray(long N)
        {
            var inputStrArray = new string[N];
            for (long i = 0; i < N; ++i)
            {
                inputStrArray[i] = Read;
            }

            return inputStrArray;
        }

        public int[] ReadIntArray(long N)
        {
            var inputIntArray = new int[N];
            for (long i = 0; i < N; ++i)
            {
                inputIntArray[i] = ReadInt;
            }

            return inputIntArray;
        }

        public long[] ReadLongArray(long N)
        {
            var inputLongArray = new long[N];
            for (long i = 0; i < N; ++i)
            {
                inputLongArray[i] = ReadLong;
            }

            return inputLongArray;
        }
    }
}