Getting Wrong Answer in 16th test Case

Getting Wrong Answer in 16th Test Case.
Codeforces-> EDU->Two Pointers->Step 3 G Problem.

Solution written BY me-
ll n, s;
cin >> n >> s;
string g;
cin >> g;
ll i = 0, j = 0;
map<char, ll> m;
ll ans = 0;
ll t = 0;
while (j < n) {
m[g[j]]++;
if (g[j] == ‘b’)
t += m[‘a’];
ll p = 0;
int valid = 0;
while (t > s ) {
if (g[i] == ‘b’) {
t -= p;

  }
  if (g[i] == 'a') {
    p++;
  }
  i++;
  valid = 1;
}
m['a'] -= p;
if (valid)
  i--;
if (t <= s) {

  ans = max(ans, j - i + 1);
}
j++;

}
cout << ans;

7 2
accbaba
correct o/p-6
your o/p-5

see here for a very neat solution

Thanks for Test case .I Understood what was the problem in code.