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); } }
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