PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000063. Thu điểm trên dải ô (Right Left Wrong)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Thu điểm trên dải ô (Right Left Wrong)

Right Left Wrong

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A strip has nn cells indexed from 11 to nn. Cell ii contains a positive integer aia_i and a character sis_i, either L or R. Initially the score is 00. In one operation, choose l<rl<r with L at position ll and R at position rr. Add al+⋯+ara_l+\cdots+a_r to the score, then replace every character in [l,r][l,r] with a dot. Such positions cannot be selected as endpoints later. Maximize the total score over any number of operations.

Input

  • The first line contains nn.
  • The second line contains nn integers a1,…,ana_1,\ldots,a_n.
  • The third line contains a string ss of length nn consisting of L and R.

Output

Print a single integer: the maximum obtainable score.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le 20; 1≤ai≤1051\le a_i\le10^5 for every ii; si∈{L,R}s_i\in\{\texttt{L},\texttt{R}\} for every ii.
  • Subtask 2 (30 points): 2≤n≤20002\le n\le 2000; 1≤ai≤1051\le a_i\le10^5 for every ii; si∈{L,R}s_i\in\{\texttt{L},\texttt{R}\} for every ii.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; 1≤ai≤1051\le a_i\le10^5 for every ii; si∈{L,R}s_i\in\{\texttt{L},\texttt{R}\} for every ii.

Examples

Input

6
3 5 1 4 3 2
LRLLLR

Output

18

Explanation

The array is [3,5,1,4,3,2][3,5,1,4,3,2] and the character string is LRLLLR. Choosing l=1,r=6l=1,r=6 is valid because the endpoints are L and R. This operation yields 3+5+1+4+3+2=183+5+1+4+3+2=18. Every position becomes a dot afterward. As the sum of all cells is 1818 and a cell cannot be scored twice, no higher score is possible. Therefore the answer is 18.