Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the ith balloon.

Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful. You are given a 0-indexed integer array neededTime where neededTime[i] is the time (in seconds) that Bob needs to remove the ith balloon from the rope.

Return the minimum time Bob needs to make the rope colorful.

&nbsp;
Example 1:

Input: colors = "abaac", neededTime = [1,2,3,4,5]
Output: 3
Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
Bob can remove the blue balloon at index 2. This takes 3 seconds.
There are no longer two consecutive balloons of the same color. Total time = 3.

Example 2:

Input: colors = "abc", neededTime = [1,2,3]
Output: 0
Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.


Example 3:

Input: colors = "aabaa", neededTime = [1,2,3,4,1]
Output: 2
Explanation: Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.


&nbsp;
Constraints:


	n == colors.length == neededTime.length
	1 &lt;= n &lt;= 105
	1 &lt;= neededTime[i] &lt;= 104
	colors contains only lowercase English letters.


class Solution:
    def minCost(self, s: str, cost: List[int]) -> int:
        ans = prev = 0 # index of previously retained letter 
        for i in range(1, len(s)): 
            if s[prev] != s[i]: prev = i
            else: 
                ans += min(cost[prev], cost[i])
                if cost[prev] < cost[i]: prev = i
        return ans 
class Solution {
    public int minCost(String colors, int[] neededTime) {
        return minCost(colors, neededTime, 0, neededTime.length - 1);
    }
    
    public int minCost(String colors, int[] neededTime, int start, int end) {
        if (start == end) {
            return 0;
        }
        
        int mid = (start + end) / 2;
        int lEnd = mid;
        int rStart = mid + 1;
        int t1 = minCost(colors, neededTime, start, lEnd);
        int t2 = minCost(colors, neededTime, rStart, end);
        
        while (neededTime[lEnd] < 0 && lEnd >= start) {
            --lEnd;
        }
        while (neededTime[rStart] < 0 && rStart <= end) {
            ++rStart;
        }
        
        if (colors.charAt(lEnd) != colors.charAt(rStart)) {
            return t1 + t2;
        }
        
        int removeTime = 0;
        if (neededTime[lEnd] <= neededTime[rStart]) {
            removeTime = neededTime[lEnd];
            neededTime[lEnd] *= -1;
        }
        else {
            removeTime = neededTime[rStart];
            neededTime[rStart] *= -1;
        }
            
        return t1 + t2 + removeTime;
    }
}
class Solution {
public:
    int minCost(string colors, vector<int>& neededTime) {
        int time=0;
        for(int i=0;i<colors.length()-1;i++){
            if(colors[i]==colors[i+1]){
                if(neededTime[i]>neededTime[i+1]){
                    swap(neededTime[i],neededTime[i+1]);
                }
                time+=min(neededTime[i],neededTime[i+1]);
            }
        }return time;
    }
};
var minCost = function(colors, neededTime) {
    let stack = [], time = 0;
    for (let i = 0; i < colors.length; i++) {
        let skipPush = false;
        while (stack.length && colors[i] === colors[stack[stack.length - 1]]) {
            if (neededTime[i] >= neededTime[stack[stack.length - 1]]) {
                time += neededTime[stack.pop()];
            }
            else if (neededTime[i] < neededTime[stack[stack.length - 1]]) {
                time += neededTime[i];
                skipPush = true;
                break;
            }
        }
        if (!skipPush) stack.push(i);
    }
    return time;
};
Minimum Time to Make Rope Colorful