## https://yukicoder.me/problems/no/1288 import heapq def main(): N = int(input()) S = input() V = list(map(int, input().split())) i_queue = [] k_queue = [] u_queue = [] answer = 0 for i in reversed(range(N)): s = S[i] if s == "i": heapq.heappush(i_queue, -V[i]) elif s == "k": if len(i_queue) > 0: v = heapq.heappop(i_queue) v = -v heapq.heappush(k_queue, -(V[i] + v)) elif s == "u": if len(k_queue) > 0: v = heapq.heappop(k_queue) v = -v heapq.heappush(u_queue, -(V[i] + v)) else: if len(u_queue) > 0: v = heapq.heappop(u_queue) v = -v answer += v + V[i] print(answer) if __name__ == "__main__": main()