Time limit時間制限 : 2sec / Memory limitメモリ制限 : 256MB
配点 : 1400 点
高橋君は N 項からなる数列 (A_1,A_2,...,A_N) を拾いました。数列を全部持ち帰るのは大変なので、高橋君は圧縮して一つの数にすることに決めました。
圧縮は N-1 回のステップからなり、各ステップごとに今ある数列を、長さが 1 短い数列に圧縮します。ステップでの操作を表す文字列を S として、今ある数列を (a_1,a_2,...,a_K) とするとき、i 回目のステップでは以下の操作を行います。
M
のとき、b_i = max(a_i,a_{i+1}) (1 ≦ i ≦ K-1) として、数列を (b_1,b_2,...,b_{K-1}) に圧縮するm
のとき、b_i = min(a_i,a_{i+1}) (1 ≦ i ≦ K-1) として、数列を (b_1,b_2,...,b_{K-1}) に圧縮するさて、高橋君は圧縮するステップの操作を表す文字列 S を決めましたが、疲れていて実際に圧縮することができません。代わりに、圧縮して得られる数を求めてください。
M
または m
である。M
、その他の文字が m
である。すなわち、S は M...Mm...m
のようになる。M
、偶数文字目は m
である。すなわち、S は MmMmMm...
のようになる。入力は以下の形式で標準入力から与えられる。
N A_1 A_2 ... A_N S
圧縮して高橋君が得られる数を出力せよ。
4 1 2 3 4 MmM
3
最初の数列が ( 1 , 2 , 3 , 4 ) なので、
よって、求める数は 3 です。
5 3 4 2 2 1 MMmm
2
10 1 8 7 6 8 5 2 2 6 1 MmmmmMMMm
5
20 12 7 16 8 7 19 8 19 20 11 7 13 20 3 4 11 19 11 15 5 mMMmmmMMMMMMmMmmmMM
11
Score : 1400 points
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:
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}).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.
M
or m
.M
, and the remaining characters are m
, that is, S is in the form M...Mm...m
.M
, and the even-number-th characters are m
, that is, S is in the form MmMmMm...
.The input is given from Standard Input in the following format:
N A_1 A_2 … A_N S
Print the single integer obtained from the compression.
4 1 2 3 4 MmM
3
The initial sequence ( 1 , 2 , 3 , 4 ) is compressed as follows:
Thus, the single integer obtained from the compression is 3.
5 3 4 2 2 1 MMmm
2
10 1 8 7 6 8 5 2 2 6 1 MmmmmMMMm
5
20 12 7 16 8 7 19 8 19 20 11 7 13 20 3 4 11 19 11 15 5 mMMmmmMMMMMMmMmmmMM
11