-- ordersの型が何故か[String]扱いになってしまっていて時間が足りない。setGolemを計算と初期化の両方に使っていて身動きが取れない。多分wordsが[String]を吐く関数なんだろうな、都合よく一文字StringをCharに変換する関数とかそのへんに無いかな

import Data.List
import Data.Bool

setGolem :: Char -> (Int -> Int -> Int) -> (Int -> Int) -> (Int -> Int -> Int) -> [Int] -> [Int]
setGolem xx lef fc rig [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'A' = [fc a,rig a b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'B' = [lef a b,fc b,rig b c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'C' = [a,lef b c,fc c,rig d e,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'D' = [a,b,lef c d,fc d,rig d e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'E' = [a,b,c,lef d e,fc e,rig e f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'F' = [a,b,c,d,lef e f,fc f,rig f g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'G' = [a,b,c,d,e,lef f g,fc g,rig g h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'H' = [a,b,c,d,e,f,lef g h,fc h,rig h i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'I' = [a,b,c,d,e,f,g,lef h i,fc i,rig i j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'J' = [a,b,c,d,e,f,g,h,lef i j,fc j,rig j k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'K' = [a,b,c,d,e,f,g,h,i,lef j k,fc k,rig k l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'L' = [a,b,c,d,e,f,g,h,i,j,lef k l,fc l,rig l m,n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'M' = [a,b,c,d,e,f,g,h,i,j,k,lef l m,fc m,rig m n,o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'N' = [a,b,c,d,e,f,g,h,i,j,k,l,lef m n,fc n,rig n o,p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'O' = [a,b,c,d,e,f,g,h,i,j,k,l,m,lef n o,fc o,rig o p,q,r,s,t,u,v,w,x,y,z]
 | xx == 'P' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,lef o p,fc p,rig p q,r,s,t,u,v,w,x,y,z]
 | xx == 'Q' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,lef p q,fc q,rig r q,s,t,u,v,w,x,y,z]
 | xx == 'R' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,lef q r,fc r,rig r s,t,u,v,w,x,y,z]
 | xx == 'S' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,lef r s,fc s,rig s t,u,v,w,x,y,z]
 | xx == 'T' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,lef s t,fc t,rig t u,v,w,x,y,z]
 | xx == 'U' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,lef t u,fc u,rig u v,w,x,y,z]
 | xx == 'V' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,lef u v,fc v,rig v w,x,y,z]
 | xx == 'W' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,lef v w,fc w,rig w x,y,z]
 | xx == 'X' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,lef w x,fc x,rig x y,z]
 | xx == 'Y' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,lef x y,fc y,rig y z]
 | xx == 'Z' = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,lef y z,fc z]

main = do
 _ <- getLine
 i <- getLine
 orders <- words<$>getContents
 print $ solve i orders

solve :: [Char] -> [Char] -> Int
solve i orders = move orders (golem i)where
 move [] g = sum g
 move (point:direction:next) g
  | direction == 'R' = move next (setGolem point (\a b -> a)(\a -> 0)(\a b -> a+b) g)
  | otherwise = move next (setGolem point (\a b -> a+b)(\a -> 0)(\a b -> b) g)
 golem [] = replicate 26 0
 golem (x:xs) = setGolem x (\a b -> a)(\a -> a+1)(\a b -> b) (golem xs)
