Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
543 B
1
Indexable
Never
#include<iostream>
using namespace std;

int n;
int a[100000],;

int main()
{
    cin >> n;
    char c;
    cin >> c;
    a[0] = 1;
    for (int i = 1; i < n; ++i) {
      char c_;
      cin >> c_;
      a[i] = c_ == c ? 1 : -1;
    }
    int cur_min = 0, cur_max = 0, sum = 0;
    int res = 0;
    for (int i = 0; i < n; ++i) {
      sum += a[i];
      res = max(res, sum - cur_min);
      res = max(res, cur_max - sum);
      cur_min = min(cur_min, sum);
      cur_max = max(cur_max, sum);
    }
    cout << res;
}
Leave a Comment