N=int(input()) S=input() A=list(map(int,input().split())) T=[0] for s,a in zip(S,A): if s=="R": T.append(T[-1]+a) else: T.append(T[-1]-a) U=T.copy() V=T.copy() for i in range(1,N+1): U[i]=min(U[i-1],U[i]) V[i]=max(V[i-1],V[i]) X=0 for i in range(1,N+1): X=max(X,T[i]-U[i],V[i]-T[i]) print(X)