Untitled
unknown
plain_text
a year ago
1.2 kB
4
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