<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 solve(string &s, int n, int i, int j, int cntA[], int cntB[], int x, int y) {
        if(j<=i) return 0;
        int used=0, ans=0, cnt=min(cntA[i]-cntA[j+1], cntB[i]-cntB[j+1]);
        int ca = cntA[i]-cntA[j+1];
        int cb = cntB[i]-cntB[j+1];
        if(x > y) {
            while(i<=j) {
                if(s[i] == 'a' && cb > 0) {
                    // cout<<cb<<" "<<used<<endl;
                    ans += x;
                    used++;
                    cb--;
                    cnt--;
                }
                if(s[i] == 'b') {
                    if(used>0) used--;
                    else cb--;
                }
                i++;
            }
            return ans+cnt*y;
        }
        used=0;
        while(i<=j) {
            if(s[i] == 'b' &&  ca > 0) {
                ans += y;
                used++;
                ca--;
                cnt--;
            }
            if(s[i] == 'a') {
                if(used>0) used--;
                else ca--;
            }
            i++;
        }
        // cout<<ans<<" x<y "<<cnt<<endl;
        return ans+cnt*x;
    }
    int maximumGain(string s, int x, int y) {
        int n=s.size(), ans=0, last=-1;
        int cntA[n+1], cntB[n+1], ca=0, cb=0;
        cntA[n]=cntB[n]=0;
        for(int k=n-1;k>=0;k--) {   //initialize count 
            if(s[k]=='a') ca++;
            if(s[k]=='b') cb++;
            cntA[k] = ca;
            cntB[k] = cb;
        }

        for(int i=0;i<n;i++) {
            if(s[i] != 'a' && s[i] != 'b') {
                ans += solve(s, n, last+1, i-1, cntA, cntB, x, y);
                last = i;
            }
        }
        if(s[n-1] == 'a' || s[n-1] == 'b') {
            ans += solve(s, n, last+1, n-1, cntA, cntB, x, y);
        }
        return ans;
    }
};
19259