<p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>

<ul>
	<li>Remove substring <code>&quot;ab&quot;</code> and gain <code>x</code> points.

	<ul>
		<li>For example, when removing <code>&quot;ab&quot;</code> from <code>&quot;c<u>ab</u>xbae&quot;</code> it becomes <code>&quot;cxbae&quot;</code>.</li>
	</ul>
	</li>
	<li>Remove substring <code>&quot;ba&quot;</code> and gain <code>y</code> points.
	<ul>
		<li>For example, when removing <code>&quot;ba&quot;</code> from <code>&quot;cabx<u>ba</u>e&quot;</code> it becomes <code>&quot;cabxe&quot;</code>.</li>
	</ul>
	</li>
</ul>

<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;cdbcbbaaabab&quot;, x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the &quot;ba&quot; underlined in &quot;cdbcbbaaa<u>ba</u>b&quot;. Now, s = &quot;cdbcbbaaab&quot; and 5 points are added to the score.
- Remove the &quot;ab&quot; underlined in &quot;cdbcbbaa<u>ab</u>&quot;. Now, s = &quot;cdbcbbaa&quot; and 4 points are added to the score.
- Remove the &quot;ba&quot; underlined in &quot;cdbcb<u>ba</u>a&quot;. Now, s = &quot;cdbcba&quot; and 5 points are added to the score.
- Remove the &quot;ba&quot; underlined in &quot;cdbc<u>ba</u>&quot;. Now, s = &quot;cdbc&quot; and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aabbaaxybbaabb&quot;, x = 5, y = 4
<strong>Output:</strong> 20
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= x, y &lt;= 10<sup>4</sup></code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class Solution {
public:
    int maximumGain(string s, int x, int y) {
        ios::sync_with_stdio(false);
        int i = 0;
        int ans = 0;
        int begin = 0;
        bool is_section = false;
        while(i < s.size()){
            if(is_section){
                if(s[i] != 'a' && s[i] != 'b'){
                    is_section = false;
                    ans += solve_section(string(s.begin()+begin, s.begin()+i), x, y);
                }
            }
            else if(s[i] == 'a' || s[i] == 'b'){
                is_section = true;
                begin = i;
            }
            ++i;
        }
        if(is_section) ans += solve_section(string_view(s.begin()+begin, s.end()), x, y);
        return ans;
    }
private:
    int solve_section(string_view closed_section, int x, int y){
        if(x >= y){
            int left_over_as = 0;
            int left_over_bs = 0;
            int ans = 0;
            for(int i = 0; i < closed_section.size(); ++i){
                if(closed_section[i] == 'a'){
                    ++left_over_as;
                }
                else if(left_over_as != 0){
                    --left_over_as;
                    ans += x;
                }
                else{
                    ++left_over_bs;
                }
            }
            return ans + (min(left_over_as, left_over_bs) * y);
        }
        else{
            int left_over_as = 0;
            int left_over_bs = 0;
            int ans = 0;
            for(int i = 0; i < closed_section.size(); ++i){
                if(closed_section[i] == 'b'){
                    ++left_over_bs;
                }
                else if(left_over_bs != 0){
                    --left_over_bs;
                    ans += y;
                }
                else{
                    ++left_over_as;
                }
            }
            return ans + (min(left_over_as, left_over_bs) * x);
        }
    }
};
19678