<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:
    def maximumGain(self, s: str, x: int, y: int) -> int:      
        score = 0
        if x > y:
            rep1, rep2 = "ab", "ba"
            add1, add2 = x, y
        else:
            rep1, rep2 = "ba", "ab"
            add1, add2 = y, x
        
        s2 = s
        while True:
            s1 = ""
            i, j, ir1, ir2 = 0,0,0,0
            while i<len(s2):
                if s2[i:i+2] == rep1:
                    s1 = s1[:ir1] + s2[ir2:i]
                    j = i-1
                    i = i+2
                    score += add1
                    while i<len(s2) and len(s1)>0 and j<len(s1) and j>=0:
                        if s1[j] + s2[i] == rep1:
                            i += 1
                            j -= 1
                            score += add1
                        else:
                            break
                    ir1 = j+1
                    ir2 = i
                else:
                    i += 1
            s1 = s1[:ir1] + s2[ir2:i]
            if s1 == s2:
                break
            else:
                s2 = s1
        while True:
            s1 = ""
            i, j, ir1, ir2 = 0, 0, 0, 0
            while i<len(s2):
                if s2[i:i+2] == rep2:
                    s1 = s1[:ir1] + s2[ir2:i]
                    j = i-1
                    i = i+2
                    score += add2
                    while i<len(s2) and len(s1)>0 and j<len(s1) and j>=0:
                        if s1[j] + s2[i] == rep2:
                            i += 1
                            j -= 1
                            score += add2
                        else:
                            break
                    ir1 = j+1
                    ir2 = i
                else:
                    i += 1
            s1 = s1[:ir1] + s2[ir2:i]
            if s1 == s2:
                break
            else:
                s2 = s1
        return score
        
17800