This page is still under construction.

Parts of this page are still being built. What you see may change.

Minuses

Time limit1sMemory limit128 MB

Summary
Given a signed sum of distinct variables, find the fewest bracket pairs needed to turn the all-minus chain into an equivalent expression.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Math, Implementation
Solved
No attempts yet

Problem

Subtraction is not associative. For example (5−2)−1=2(5-2)-1=2 but 5−(2−1)=45-(2-1)=4, so (5−2)−1≠5−(2−1)(5-2)-1 \ne 5-(2-1). This means the value of an expression such as 5−2−15-2-1 depends on the order in which the subtractions are performed. When there are no brackets we agree to evaluate from left to right, so 5−2−15-2-1 means (5−2)−1(5-2)-1.

You are given an expression of the form x1±x2±⋯±xn,x_1 \pm x_2 \pm \dots \pm x_n, where each ±\pm is either ++ or −-, and x1,x2,…,xnx_1, x_2, \dots, x_n are pairwise distinct variables.

We want to insert brackets into the all-minus expression x1−x2−⋯−xnx_1 - x_2 - \dots - x_n so that it becomes equivalent to the given expression (the two expressions are equal for every value of the variables). At most n−1n-1 pairs of brackets may be inserted, and no pair of brackets may enclose zero or one variable.

For example, to match x1−x2−x3+x4+x5−x6+x7x_1 - x_2 - x_3 + x_4 + x_5 - x_6 + x_7 we may bracket x1−x2−x3−x4−x5−x6−x7x_1 - x_2 - x_3 - x_4 - x_5 - x_6 - x_7 as ((x1−x2)−(x3−x4−x5))−(x6−x7).((x_1-x_2)-(x_3-x_4-x_5))-(x_6-x_7). This is only one of many possible bracketings, and it is not necessarily one that uses the fewest pairs. Determine the minimum number of bracket pairs over all valid bracketings.

Input

The first line contains an integer nn (2≤n≤1062 \le n \le 10^6), the number of variables in the given expression. Each of the next n−1n-1 lines contains a single character, ++ or −-. The kk-th of these lines (1≤k≤n−11 \le k \le n-1) is the sign between xkx_k and xk+1x_{k+1} in the given expression. You may assume that a valid bracketing always exists for the input.

Output

Print a single integer: the minimum number of bracket pairs that must be inserted into x1−x2−⋯−xnx_1 - x_2 - \dots - x_n to obtain an expression equivalent to the given one.

Examples3

  1. Example 1

    Input
    7
    -
    -
    +
    +
    -
    +
    
    Expected output
    2
    
  2. Example 2

    Input
    2
    -
    
    Expected output
    0
    
  3. Example 3

    Input
    4
    -
    +
    +
    
    Expected output
    1