1717.cpp (664B)
1 class Solution { 2 public: 3 int maximumGain(const string s, int x, int y) const { 4 int res = 0, a = 0, b = 0, mini = min(x, y); 5 6 for (const char c : s) { 7 if (c > 'b') { 8 res += min(a, b) * mini; 9 a = b = 0; 10 continue; 11 } 12 13 if (c == 'a') { 14 if (x < y && b > 0) 15 b--, res += y; 16 else 17 a++; 18 } else { 19 if (x > y && a > 0) 20 a--, res += x; 21 else 22 b++; 23 } 24 } 25 26 return res + min(a, b) * mini; 27 } 28 };