import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String string = sc.next();
        int length = string.length();
        int res = 0;

        int[] values = new int[length];
        for (int i = 0; i < length; i++) {
            values[i] = sc.nextInt();
        }

        int lastDigit = string.charAt(0) - '0';
        int previousValue = values[0];

        for (int i = 1; i < length; i++) {
            int currentDigit = string.charAt(i) - '0';
            if (currentDigit == lastDigit) {
                res += Math.min(previousValue, values[i]);
                previousValue = Math.max(previousValue, values[i]);
            } else {
                lastDigit = currentDigit;
                previousValue = values[i];
            }
        }

        System.out.println(res);
    }
}