int L, R = 1, ans = 0; for(L = 1; L <= n; L = R + 1) { for(R = L; R + 1 <= n && B[L] == B[R + 1]; R++); ans = max(ans, R - L + 1); }