classSolution { public: intminCost(string colors, vector<int> &neededTime) { int i = 0, j=1, n = colors.size(); int res = 0; int max_time; int total; while (j < n) { j = i + 1; total = neededTime[i]; max_time = neededTime[i]; while (colors[i] == colors[j]) { max_time = std::max(neededTime[j], max_time); total += neededTime[j]; j++; } if (total == neededTime[i]) { i++; } else { res += total - max_time; i = j; } }