Problem:
Score : 1400 points

Problem Statement

Takahashi found an integer sequence (A_1,A_2,...,A_N) with N terms. Since it was too heavy to carry, he decided to compress it into a single integer.

The compression takes place in N-1 steps, each of which shorten the length of the sequence by 1. Let S be a string describing the steps, and the sequence on which the i-th step is performed be (a_1,a_2,...,a_K), then the i-th step will be as follows:

When the i-th character in S is M, let b_i = max(a_i,a_{i+1}) (1 ≦ i ≦ K-1), and replace the current sequence by (b_1,b_2,...,b_{K-1}).

When the i-th character in S is m, let b_i = min(a_i,a_{i+1}) (1 ≦ i ≦ K-1), and replace the current sequence by (b_1,b_2,...,b_{K-1}).

Takahashi decided the steps S, but he is too tired to carry out the compression. On behalf of him, find the single integer obtained from the compression.

Constraints

2 ≦ N ≦ 10^5

1 ≦ A_i ≦ N

S consists of N-1 characters.

Each character in S is either M or m.

Partial Scores

In the test set worth 400 points, there exists i (1 ≦ i < N-1) such that the first i characters in S are M, and the remaining characters are m, that is, S is in the form M...Mm...m.

In the test set worth 800 points, the odd-number-th characters in S are M, and the even-number-th characters are m, that is, S is in the form MmMmMm....

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N
S

Output

Print the single integer obtained from the compression.

Sample Input 1

4
1 2 3 4
MmM

Sample Output 1

3

The initial sequence ( 1 , 2 , 3 , 4 ) is compressed as follows:

After the first step, the sequence is ( 2 , 3 , 4 ).

After the second step, the sequence is ( 2 , 3 ).

After the third step, the sequence is ( 3 ).

Thus, the single integer obtained from the compression is 3.

Sample Input 2

5
3 4 2 2 1
MMmm

Sample Output 2

2

Sample Input 3

10
1 8 7 6 8 5 2 2 6 1
MmmmmMMMm

Sample Output 3

5

Sample Input 4

20
12 7 16 8 7 19 8 19 20 11 7 13 20 3 4 11 19 11 15 5
mMMmmmMMMMMMmMmmmMM

Sample Output 4

11

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::*;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.is_err() || res.ok().unwrap() == 0 || u8b[0] <= ' ' as u8 {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = std::string::String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}
fn parse<T: std::str::FromStr>(s: &str) -> T { s.parse::<T>().ok().unwrap() }

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { parse(&get_word()) }

/**
 * Sparse Table.
 * BiOp should be the type of a binary operator which is
 * associative, commutative and idempotent.
 * (For example, both min and gcd satisfy these properties.)
 */
struct SparseTable<T, BiOp> {
    biop: BiOp,
    st: Vec<Vec<T>>,
}

impl<T, BiOp> SparseTable<T, BiOp>
    where BiOp: Fn(T, T) -> T,
          T: Copy {
    pub fn new(ary: &[T], biop: BiOp) -> Self {
        let n = ary.len();
        let mut h = 1;
        while 1 << h < n {
            h += 1;
        }
        let mut st: Vec<Vec<T>> = vec![Vec::from(ary); h + 1];
        for i in 0 .. n {
            st[0][i] = ary[i];
        }
        for b in 1 .. (h + 1) {
            if n + 1 < 1 << b {
                break;
            }
            for i in 0 .. (n + 1 - (1 << b)) {
                let next_idx = (1 << (b - 1)) + i;
                st[b][i] = biop(st[b - 1][i], st[b - 1][next_idx]);
            }
        }
        SparseTable {biop: biop, st: st}
    }
    fn top_bit(t: usize) -> usize {
        let mut h = 0;
        while 1 << h <= t {
            h += 1;
        }
        h - 1
    }
    pub fn query(&self, f: usize, s: usize) -> T {
        assert!(f <= s);
        let b = Self::top_bit(s + 1 - f);
        let endpoint = s + 1 - (1 << b);
        (self.biop)(self.st[b][f], self.st[b][endpoint])
    }
}

fn sol_spt(n: usize, a: &[i64], s: &[char]) -> Option<i64> {
    let mut idx = 0;
    for i in 0 .. (n - 1) {
        if s[i] == 'm' {
            idx = i;
            break;
        }
    }
    for i in 0 .. idx {
        if s[i] != 'M' {
            return None;
        }
    }
    for i in idx .. (n - 1) {
        if s[i] != 'm' {
            return None;
        }
    }
    let spt: SparseTable<i64, _> = SparseTable::new(&a, |x, y| max(x, y));
    let mut b: Vec<i64> = vec![0; n - idx];
    for i in 0 .. (n - idx) {
        b[i] = spt.query(i, i + idx);
    }
    let spt2: SparseTable<i64, _> = SparseTable::new(&b, |x, y| min(x, y));
    return Some(spt2.query(0, n - idx - 1));
}

fn main() {
    let n: usize = get();
    let a: Vec<i64> = (0 .. n).map(|_| get()).collect();
    let s: String = get_word();
    let sc: Vec<char> = s.chars().collect();
    let res1 = sol_spt(n, &a, &sc);
    println!("{}", res1.unwrap());
}