PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000125. Chuỗi có triển vọng – phiên bản khó (Promising String (Hard Version))

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chuỗi có triển vọng – phiên bản khó (Promising String (Hard Version))

Promising String (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A nonempty string of + and - characters is balanced if it contains equally many plus and minus signs. One operation replaces two adjacent minus signs (--) by a single plus sign (+). The string becomes one character shorter.

A nonempty string is promising if zero or more operations can make it balanced. Given a string ss of length nn, count all nonempty contiguous substrings that are promising. Equal-looking substrings at different positions count separately.

For example, --- is promising since --- can become +-, which is balanced, while -- is not. Noncontiguous subsequences do not count.

Input

  • First line: nn, the string length.
  • Second line: a string ss of exactly nn characters, each + or -.

Output

Print the number of nonempty promising substrings as one integer.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; Strings consist only of plus and minus signs.
  • Subtask 2 (30 points): 1≤n≤40001\le n\le 4000; Strings consist only of plus and minus signs.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; Strings consist only of plus and minus signs.

Examples

Example 1

Input

3
+-+

Output

2

Explanation

The two length-two substrings +- at positions 1–2 and -+ at positions 2–3 are already balanced. Neither a one-character substring nor +-+ can become balanced, so the answer is 2.

Example 2

Input

5
-+---

Output

4

Explanation

The four valid substrings are [1,2] = -+, [2,3] = +-, [3,5] = ---, and [1,5] = -+---. The first two are balanced, --- becomes +-, and -+--- becomes -++-. Thus the answer is 4.