import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String string = br.readLine(); int length = string.length(); String[] input = br.readLine().split(" "); int[] values = new int[length]; for (int i = 0; i < length; i++) { values[i] = Integer.parseInt(input[i]); } int res = 0; 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); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter