Untitled
unknown
plain_text
5 months ago
1.2 kB
3
Indexable
import java.util.*; public class AlternateString { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String orgSeq = scn.nextLine(); int seqLen = orgSeq.length(); int[] tCost = new int[seqLen]; for (int i = 0; i < seqLen; i++) { tCost[i] = scn.nextInt(); } int totalMinCost = calculateMinimumAlternateCost(orgSeq, tCost); System.out.print(totalMinCost); scn.close(); } private static int calculateMinimumAlternateCost(String sseq, int[] costs) { int minCost = 0; int currentDigit = sseq.charAt(0) - '0'; int currentCost = costs[0]; for (int i = 1; i < sseq.length(); i++) { int nextDigit = sseq.charAt(i) - '0'; if (nextDigit == currentDigit) { minCost += Math.min(currentCost, costs[i]); currentCost = Math.max(currentCost, costs[i]); } else { currentDigit = nextDigit; currentCost = costs[i]; } } return minCost; } }
Editor is loading...
Leave a Comment