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 Rust (1.42.0) (judge verdict: Accepted)

#![allow(unused_macros)]
#![allow(dead_code)]
#![allow(unused_imports)]

use itertools::Itertools;
use proconio::*;
use std::collections::VecDeque;
use std::io::stdin;
use std::str::FromStr;
use text_io::*;

const U_INF: usize = 1 << 60;
const I_INF: isize = 1 << 60;

fn main() {
    let mut sc = Scanner::new();
    let n = sc.next_usize();

    let mut now = 1000;
    let mut a = Vec::with_capacity(n);
    for _ in 0..n {
        let ai = sc.next_usize();
        a.push(ai);
    }

    for i in 0..n - 1 {
        if a[i] < a[i + 1] {
            let b = now / a[i];
            now -= b * a[i];
            now += b * a[i + 1];
        }
    }

    println!("{}", now);
}

use std::fmt::{Display, Formatter};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

pub const MOD: isize = 1000000007;
// pub const MOD: isize = 998244353;

/// 常に設定したModを取り続ける正整数型
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct ModInt {
    /// 保持する値
    value: isize,
}

impl ModInt {
    /// isizeを受け取り、ModIntに変換する
    pub fn new(n: isize) -> Self {
        let mut value = n % MOD;
        if value < 0 {
            value += MOD;
        }
        Self { value }
    }

    /// べき乗計算関数
    /// 二分累乗法を用いるため、計算量はO(log n)
    pub fn pow(self, n: usize) -> Self {
        if n == 0 {
            1.into()
        } else if n == 1 {
            self
        } else if n % 2 == 0 {
            (self * 2.into()).pow(n / 2)
        } else {
            self * self.pow(n - 1)
        }
    }

    /// 逆元を返す
    /// フェルマーの少定理より、べき乗を用いて計算するため、計算量はO8log MOD)
    pub fn inv(self) -> Self {
        self.pow((MOD - 2) as usize)
    }
}

impl Display for ModInt {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.value)
    }
}

impl From<usize> for ModInt {
    fn from(n: usize) -> Self {
        Self::new(n as isize)
    }
}

impl From<u64> for ModInt {
    fn from(n: u64) -> Self {
        Self::new(n as isize)
    }
}

impl From<u32> for ModInt {
    fn from(n: u32) -> Self {
        Self::new(n as isize)
    }
}

impl From<u16> for ModInt {
    fn from(n: u16) -> Self {
        Self::new(n as isize)
    }
}

impl From<u8> for ModInt {
    fn from(n: u8) -> Self {
        Self::new(n as isize)
    }
}

impl From<isize> for ModInt {
    fn from(n: isize) -> Self {
        Self::new(n)
    }
}

impl From<i64> for ModInt {
    fn from(n: i64) -> Self {
        Self::new(n as isize)
    }
}

impl From<i32> for ModInt {
    fn from(n: i32) -> Self {
        Self::new(n as isize)
    }
}

impl From<i16> for ModInt {
    fn from(n: i16) -> Self {
        Self::new(n as isize)
    }
}

impl From<i8> for ModInt {
    fn from(n: i8) -> Self {
        Self::new(n as isize)
    }
}

impl Add for ModInt {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let mut tmp = self.value + rhs.value;
        if tmp >= MOD {
            tmp %= MOD;
        }

        Self { value: tmp }
    }
}

impl AddAssign for ModInt {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs
    }
}

impl Sub for ModInt {
    type Output = ModInt;

    fn sub(self, rhs: Self) -> Self::Output {
        let mut tmp = self.value;
        if tmp < rhs.value {
            tmp += MOD;
        }
        tmp -= rhs.value;
        if tmp >= MOD {
            tmp %= MOD;
        }

        Self { value: tmp }
    }
}

impl SubAssign for ModInt {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl Mul for ModInt {
    type Output = ModInt;

    fn mul(self, rhs: Self) -> Self::Output {
        let mut tmp = self.value * rhs.value;
        if tmp >= MOD {
            tmp %= MOD;
        }

        Self { value: tmp }
    }
}

impl MulAssign for ModInt {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl Div for ModInt {
    type Output = ModInt;

    fn div(self, rhs: Self) -> Self::Output {
        self * rhs.inv()
    }
}

impl DivAssign for ModInt {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

/// 高速に二項係数を求める構造体
pub struct Combination {
    /// `0..=n`分の階乗値を保持
    factorial: Vec<ModInt>,
    /// `0..=n`分の階乗値の逆元を保持
    factorial_inv: Vec<ModInt>,
}

impl Combination {
    /// 初期化処理
    /// 計算量はO(n)
    pub fn new(n: usize) -> Self {
        let mut factorial = Vec::with_capacity(n + 1);
        let mut factorial_inv = Vec::with_capacity(n + 1);

        factorial.push(ModInt::new(1));

        for i in 1..=n {
            factorial.push(factorial[i - 1] * (i as i32).into());
        }

        factorial_inv.push(factorial[n].inv());

        for i in (0..n).rev() {
            factorial_inv[i] = factorial_inv[i + 1] * ((i + 1) as i32).into();
        }

        let factorial_inv = factorial_inv.into_iter().rev().collect_vec();

        Self {
            factorial_inv,
            factorial,
        }
    }

    /// 実際のCombinationの計算
    /// 計算量はO(1)
    pub fn combination(&self, n: usize, r: usize) -> ModInt {
        if n < r {
            0.into()
        } else {
            self.factorial[n] * (self.factorial_inv[r] * self.factorial_inv[n - r])
        }
    }
}

pub struct Scanner {
    buf: VecDeque<String>,
}

impl Scanner {
    pub fn new() -> Self {
        Self {
            buf: VecDeque::new(),
        }
    }

    fn scan_line(&mut self) {
        let mut flag = 0;
        while self.buf.is_empty() {
            let mut s = String::new();
            stdin().read_line(&mut s).unwrap();
            let mut iter = s.split_whitespace().peekable();
            if iter.peek().is_none() {
                if flag >= 5 {
                    panic!("There is no input!");
                }
                flag += 1;
                continue;
            }

            for si in iter {
                self.buf.push_back(si.to_string());
            }
        }
    }

    pub fn next<T: FromStr>(&mut self) -> T {
        self.scan_line();
        self.buf
            .pop_front()
            .unwrap()
            .parse()
            .unwrap_or_else(|_| panic!("Couldn't parse!"))
    }

    pub fn next_usize(&mut self) -> usize {
        self.next()
    }

    pub fn next_isize(&mut self) -> isize {
        self.next()
    }

    pub fn next_chars(&mut self) -> Vec<char> {
        self.next::<String>().chars().collect_vec()
    }

    pub fn next_string(&mut self) -> String {
        self.next()
    }

    pub fn fill_vec_line<T: FromStr>(&mut self, v: &mut Vec<T>) {
        for vi in v {
            *vi = self.next();
        }
    }

    pub fn fill_vec<T: FromStr>(&mut self, v: &mut Vec<Vec<T>>) {
        for vi in v {
            for vii in vi {
                *vii = self.next();
            }
        }
    }

    pub fn make_vec_line<T: FromStr + Default + Clone>(&mut self, i: usize) -> Vec<T> {
        let mut v = vec![Default::default(); i];
        self.fill_vec_line(&mut v);
        v
    }

    pub fn make_vec<T: FromStr + Default + Clone>(&mut self, i: usize, j: usize) -> Vec<Vec<T>> {
        let mut v = vec![vec![Default::default(); j]; i];
        self.fill_vec(&mut v);
        v
    }
}