#include #include using namespace std; template void print(const T t, bool ln = true) { cout << t; if (ln) { cout << '\n'; } else { cout << ' '; } } template void printv(const vector &v) { for (int i = 0; i < (int)v.size(); i++) { print(v[i], false); } cout << '\n'; } template void printvv(const vector> &vv) { for (int i = 0; i < (int)vv.size(); i++) { vprint(vv[i]); } } void solve() { int n; cin >> n; string s; cin >> s; vector normal(n + 1, 1), reverse(n + 1, 1); for (int i = 0; i < n; i++) { if (s[i] == '+') { reverse[i + 1] = -1; } else if (s[i] == '-') { normal[i + 1] = -1; } normal[i + 1] += normal[i]; reverse[i + 1] += reverse[i]; } int now = 0, l = 0, cnt = 0; while (l < n) { int r = -1; for (int i = l + 1; i <= n; i++) { if (now + normal[i] - normal[l] < 0) { r = i; break; } } if (r == -1) { break; } cnt++; int mx = -1; for (int m = l + 1; m < r; m++) { if (mx < now + (normal[m] - normal[l]) + (reverse[r] - reverse[m])) { mx = now + (normal[m] - normal[l]) + (reverse[r] - reverse[m]); } } // [0, r)で最大 now = mx; l = r; for (r++; r <= n; r++) { if (now + (reverse[r] - reverse[l]) < 0) { break; } } if (r == n + 1) { break; } mx = -1; for (int m = l + 1; m < r; m++) { if (mx < now + (reverse[m] - reverse[l]) + (normal[r] - normal[m])) { mx = now + (reverse[m] - reverse[l]) + (normal[r] - normal[m]); } } l = r; } cout << cnt << '\n'; } int main() { int q = 1; // cin >> q; while (q--) { solve(); } }