Problem:
Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

Initially, he has 1000 yen and no stocks.

Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.

Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.

Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.

Day 4: Do nothing.

Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.

Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.

Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.

Solution in C# (Mono-csc 3.5.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Console;
using static System.Math;
namespace AtCoder_1
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = CinT();
            var A = CinTAr();
            var Q = new List<int>() { A[0] };
            bool preupper = false;
            for(int i = 1;i < N; i++)
            {
                if(A[i - 1] != A[i])
                {
                    if (i > 1)
                    {
                        bool q = A[i - 1] < A[i];
                        if (q == preupper)
                        {
                            Q.RemoveAt(Q.Count - 1);
                        }
                        else
                        {
                            preupper = q;
                        }

                    }
                    else
                    {
                        preupper = A[0] < A[1];
                    }
                    Q.Add(A[i]);
                }
            }
            N = Q.Count;
            A = Q.ToArray();
            Queue<int> downi = new Queue<int>();
            downi.Enqueue(0);
            Queue<int> upi = new Queue<int>();
            for(int i = 1;i < N; i++)
            {
                if(A[i - 1] < A[i])
                {
                    upi.Enqueue(i);
                }
                else if(A[i - 1] > A[i])
                {
                    downi.Enqueue(i);
                }
            }
            //CoutAr(A);
            //CoutAr(downi);
            //CoutAr(upi);
            long money = 1000;
            long stock = 0;
            long next_up_i = (upi.Count > 0) ? upi.Dequeue() : -1;
            long next_down_i = (downi.Count > 0) ? downi.Dequeue() : -1;
            for (int i = 0; i < N; i++)
            {
                //Cout(next_up_i);
                //Cout(next_down_i);
                if ((next_up_i > next_down_i) && next_down_i != -1)
                {
                    //buy timing
                    int m = A[i];
                    //Cout("buy : " + i);
                    while (money - m >= 0)
                    {
                        stock++;
                        money -= m;
                    }
                }
                if ((next_down_i > next_up_i || next_down_i == -1) && next_up_i != -1)
                {
                    //Cout("sale : " + i);
                    //sale timing
                    money += stock * A[i];
                    stock = 0;
                }
                if (i == next_down_i)
                {
                    next_down_i = (downi.Count > 0) ? downi.Dequeue() : -1;
                }
                if (i == next_up_i)
                {
                    next_up_i = (upi.Count > 0) ? upi.Dequeue() : -1;
                }
            }

            Cout(money);
            Cin();
        } //main



        static readonly int MOD10_9_7 = (1000000000 + 7);
        static Random Rand = new Random();
        static readonly string[] AlphabetTable = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        static int GetDigits(long num) { return (int)Floor(Log10(num)) + 1; }
        static int GetNumByDigit(long num, int digit)
        {
            return (int)((num / ((int)Pow(10, digit - 1))) % 10);
        }
        static int Loop(int num, int max)
        {
            if (num > max) { return num % max - 1; }
            else if (num < 0) { return max - (-num) % max + 1; }
            else { return num; }
        }
        static long PW10(int n) { return (long)Pow(10, n); }
        static long GCD(long a, long b)
        {
            if (a < b) { return GCD(b, a); }
            if (a % b == 0) { return b; }
            else { return GCD(b, a % b); }
        }
        static long LCM(long a, long b) { return a * b / GCD(a, b); }
        static void Swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
        static void SwapArE<T>(T[] a, int i, int j) { T t = a[i]; a[i] = a[j]; a[j] = t; }
        static void SwapLiE<T>(List<T> a, int i, int j) { T t = a[i]; a[i] = a[j]; a[j] = t; }
        static bool IsInt(double d) { return (int)d == d; }
        static string Cin() { return ReadLine(); }
        static string[] CinAr() { var q = Cin().Split(' '); return (q[0] == "") ? new string[0] : q; }
        static int CinT() { return S2i(Cin()); }
        static int[] CinTAr() { return CinAr().Select((x, i) => { return S2i(x); }).ToArray(); }
        static long CinL() { return S2L(Cin()); }
        static long[] CinLAr() { return CinAr().Select((x, i) => { return S2L(x); }).ToArray(); }
        static double CinD() { return S2D(Cin()); }
        static double[] CinDAr() { return CinAr().Select((x, i) => { return S2D(x); }).ToArray(); }
        static int S2i(string s) { return int.Parse(s); }
        static long S2L(string s) { return long.Parse(s); }
        static double S2D(string s) { return double.Parse(s); }
        static void Cout(object s) { WriteLine(s); }
        static string RevStr(string s) { return new string(s.Reverse().ToArray()); }
        static long[] Divisor(long n)
        {
            if (n == 1) { return new long[] { 1 }; }
            List<long> res = new List<long>() { 1, n };
            for (long i = 2; i <= Math.Sqrt(n); i++)
            {
                if (n % i == 0)
                {
                    res.Add(i);
                    long f = n / i;
                    if (f != i) { res.Add(f); }
                }
            }
            return res.ToArray();
        }
        static IEnumerable<decimal> PrimeFactors(decimal n)
        {
            decimal i = 2, tmp = n;
            while (i * i <= n)
            {
                if (tmp % i == 0)
                {
                    tmp /= i;
                    yield return i;
                }
                else { i++; }
            }
            if (tmp != 1) { yield return tmp; }
        }
        static bool IsPrime(long num)
        {
            if (num == 1) { return false; }
            for (long i = 2; i <= Sqrt(num) + 1; i++) { if (num % i == 0) { return false; } }
            return true;
        }
        static void CoutAr<Type>(IEnumerable<Type> types) { Cout(string.Join(" ", types)); }
        static long nCr(int n, int r) { return nPr(n, r) / Factorial(r); }
        static long nPr(int n, int r) { return FactorialDivision(n, n - r); }
        static long FactorialDivision(int tf, int df)
        {
            long r = 1;
            for (int i = tf; i > df; i--) { r *= i; }
            return r;
        }
        static long Factorial(int i)
        {
            if (i <= 1) { return 1; }
            return i * Factorial(i - 1);
        }
        static void CoutMemPairAr<Type>(IEnumerable<Pair<Type>> pairs)
        {
            var ar = pairs.ToArray();
            for (int i = 0; i < ar.Length; i++) { Write($"({ar[i].a}, {ar[i].b}, {ar[i].c}), "); }
            Cout("");
        }
    }
    public struct Pair<T>
    {
        public T a, b, c;
        public Pair(T _a, T _b, T _c = default(T)) { a = _a; b = _b; c = _c; }
    }
}