Problem:
Score : 200 points

Problem Statement

You have an integer variable x.
Initially, x=0.

Some person gave you a string S of length N, and using the string you performed the following operation N times.
In the i-th operation, you incremented the value of x by 1 if S_i=I, and decremented the value of x by 1 if S_i=D.

Find the maximum value taken by x during the operations (including before the first operation, and after the last operation).

Constraints

1≤N≤100

|S|=N

No characters except I and D occur in S.

Input

The input is given from Standard Input in the following format:

N
S

Output

Print the maximum value taken by x during the operations.

Sample Input 1

5
IIDID

Sample Output 1

2

After each operation, the value of x becomes 1, 2, 1, 2 and 1, respectively. Thus, the output should be 2, the maximum value.

Sample Input 2

7
DDIDDII

Sample Output 2

0

The initial value x=0 is the maximum value taken by x, thus the output should be 0.

Solution in Scala (2.11.7) (judge verdict: Accepted)

import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    //nextLineとnextを同時に使ったら死ぬ
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val s = sc.next().toCharArray

    val plus = new Array[Int](n)
    val minus = new Array[Int](n)
    for (i <- plus.indices) {
      if (i == 0) {
        s(i) match {
          case 'I' =>
            plus(i) = 1
            minus(i) = 0
          case 'D' =>
            plus(i) = 0
            minus(i) = 1
        }
      } else {
        s(i) match {
          case 'I' =>
            plus(i) = plus(i - 1) + 1
            minus(i) = minus(i - 1)
          case 'D' =>
            plus(i) = plus(i - 1)
            minus(i) = minus(i - 1) + 1
        }
      }
    }

    val ans = new Array[Int](n)
    for (i <- plus.indices) {
      ans(i) = plus(i) - minus(i)
    }
    println(math.max(ans.max, 0))
  }
}