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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

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

using Pair = System.Collections.Generic.KeyValuePair<int, int>;

class Program
{
	public Program() { }

	static void Main(string[] args)
	{
		new Program().prog();
	}
	Scanner cin;
	const int MOD = 1000000007;
	const int INF = int.MaxValue - 10;
	const long INFL = long.MaxValue - 10;
	const double EPS = 1e-7;
	const double PI = 3.1415926536;
	

	void prog()
	{
		cin = new Scanner();
		int[,] dir8 = new int[8, 2] { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, { 1, 1 } };
		int[,] dir4 = new int[4, 2] { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } };

		int N = cin.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; i++)
		{
			A[i] = cin.nextInt();
		}
		string S = cin.next();

		int M = S.Length, m = 0;
		for (int i = 0; i < S.Length; i++)
		{
			if (S[i] == 'm')
			{
				M = i;
				m = S.Length - i;
				break;
			}
		}
		SegTreeMax sgM = new SegTreeMax(A);
		int[] B = new int[N - M];
		for (int i = 0; i < N - M; i++)
		{
			B[i] = sgM.rmq(i, i + M + 1);
			//Console.WriteLine(B[i]);
		}
		//SegTreeMin sgm = new SegTreeMin(B);
		Console.WriteLine(B.Min());
	}
	class SegTreeMin
	{
		int n;  // 葉ノードの数
		int[] dat;

		// 区間[a,b)の最小値。ノードk=[l,r)に着目している。
		public int _rmq(int a, int b, int k, int l, int r)
		{
			if (r <= a || b <= l) return INF; // 交差しない
			if (a <= l && r <= b) return dat[k];    //a,l,r,bの順 
			else {
				int s1 = _rmq(a, b, 2 * k + 1, l, (l + r) / 2);
				int s2 = _rmq(a, b, 2 * k + 2, (l + r) / 2, r);
				return Math.Min(s1, s2);
			}
		}

		public int rmq(int a, int b)
		{
			return _rmq(a, b, 0, 0, n);
		}

		public void update(int k, int x)
		{
			k += n - 1;
			dat[k] = x;
			while (k > 0)
			{
				k = (k - 1) / 2;
				dat[k] = Math.Min(dat[2 * k + 1], dat[2 * k + 2]);
			}
		}

		public SegTreeMin(int size)
		{
			n = 1;
			while (n < size) n <<= 1;
			dat = new int[n << 1];
		}

		public SegTreeMin(int[] array)
		{
			n = 1;
			while (n < array.Length) n <<= 1;
			dat = new int[2 * n - 1];
			for (int i = 0; i < array.Length; i++)
			{
				dat[i + n - 1] = array[i];
			}
			for (int i = array.Length; i < n; i++)
			{
				dat[i + n - 1] = INF;
			}
			for (int i = n - 2; i >= 0; i--)
			{
				dat[i] = Math.Min(dat[2 * i + 1], dat[2 * i + 2]);
			}
		}
	}
	class SegTreeMax
	{
		int n;  // 葉ノードの数
		int[] dat;

		// 区間[a,b)の最小値。ノードk=[l,r)に着目している。
		public int _rmq(int a, int b, int k, int l, int r)
		{
			if (r <= a || b <= l) return 0; // 交差しない
			if (a <= l && r <= b) return dat[k];    //a,l,r,bの順 
			else {
				int s1 = _rmq(a, b, 2 * k + 1, l, (l + r) / 2);
				int s2 = _rmq(a, b, 2 * k + 2, (l + r) / 2, r);
				return Math.Max(s1, s2);
			}
		}

		public int rmq(int a, int b)
		{
			return _rmq(a, b, 0, 0, n);
		}

		public void update(int k, int x)
		{
			k += n - 1;
			dat[k] = x;
			while (k > 0)
			{
				k = (k - 1) / 2;
				dat[k] = Math.Max(dat[2 * k + 1], dat[2 * k + 2]);
			}
		}

		public SegTreeMax(int size)
		{
			n = 1;
			while (n < size) n <<= 1;
			dat = new int[n << 1];
		}

		public SegTreeMax(int[] array)
		{
			n = 1;
			while (n < array.Length) n <<= 1;
			dat = new int[2 * n - 1];
			for (int i = 0; i < array.Length; i++)
			{
				dat[i + n - 1] = array[i];
			}
			for (int i = array.Length; i < n; i++)
			{
				dat[i + n - 1] = 0;
			}
			for (int i = n - 2; i >= 0; i--)
			{
				dat[i] = Math.Max(dat[2 * i + 1], dat[2 * i + 2]);
			}
		}
	}
}

class Scanner
{
	string[] s;
	int i;

	char[] cs = new char[] { ' ' };

	public Scanner()
	{
		s = new string[0];
		i = 0;
	}

	public string next()
	{
		if (i < s.Length) return s[i++];
		string st = Console.ReadLine();
		while (st == "") st = Console.ReadLine();
		s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
		i = 0;
		return next();
	}

	public int nextInt()
	{
		return int.Parse(next());
	}

	public long nextLong()
	{
		return long.Parse(next());
	}

	public double nextDouble()
	{
		return double.Parse(next());
	}
}