Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Delete Files

You are using an operating system named "Jaguntu". Jaguntu provides "Filer", a file manager with a graphical user interface.

When you open a folder with Filer, the name list of files in the folder is displayed on a Filer window. Each filename is displayed within a rectangular region, and this region is called a filename region. Each filename region is aligned to the left side of the Filer window. The height of each filename region is 1, and the width of each filename region is the filename length. For example, when three files "acm.in1", "acm.c~", and "acm.c" are stored in this order in a folder, it looks like Fig. C-1 on the Filer window.

You can delete files by taking the following steps. First, you select a rectangular region with dragging a mouse. This region is called selection region. Next, you press the delete key on your keyboard. A file is deleted if and only if its filename region intersects with the selection region. After the deletion, Filer shifts each filename region to the upside on the Filer window not to leave any top margin on any remaining filename region. For example, if you select a region like Fig. C-2, then the two files "acm.in1" and "acm.c~" are deleted, and the remaining file "acm.c" is displayed on the top of the Filer window as Fig. C-3.

You are opening a folder storing $N$ files with Filer. Since you have almost run out of disk space, you want to delete unnecessary files in the folder. Your task is to write a program that calculates the minimum number of times to perform deletion operation described above.

Input

The input consists of a single test case. The test case is formatted as follows.

$N$

$D_1$ $L_1$

$D_2$ $L_2$

...

$D_N$ $L_N$

The first line contains one integer $N$ ($1 \leq N \leq 1,000$), which is the number of files in a folder. Each of the next $N$ lines contains a character $D_i$ and an integer $L_i$: $D_i$ indicates whether the $i$-th file should be deleted or not, and $L_i$ ($1 \leq L_i \leq 1,000$) is the filename length of the $i$-th file. If $D_i$ is 'y', the $i$-th file should be deleted. Otherwise, $D_i$ is always 'n', and you should not delete the $i$-th file.

Output

Output the minimum number of deletion operations to delete only all the unnecessary files.

Sample Input 1

3
y 7
y 6
n 5

Output for the Sample Input 1

1

Sample Input 2

3
y 7
n 6
y 5

Output for the Sample Input 2

2

Sample Input 3

6
y 4
n 5
y 4
y 6
n 3
y 6

Output for the Sample Input 3

2

Solution in Scala (judge verdict: Accepted)

import scala.annotation.tailrec

object Main extends App {
  import scala.io.StdIn._
  import scala.math._
  val n = readInt()
  val files = List.fill(n){
    val Array(d, l) = readLine.trim.split(' ')
    (d == "y", l.toInt)
  }
  val sanitized = sanitize(files)
  val isRemoved = Array.fill(sanitized.length){false}
  var count = 0
  for (i ← sanitized.indices.filter(i ⇒ sanitized(i)._1).sortBy(i ⇒ sanitized(i)._2) if !isRemoved(i)) {
    count += 1
    val deleteLen = sanitized(i)._2
    var left = i
    var right = i
    while (sanitized.indices.contains(left) && (sanitized(left)._1 || sanitized(left)._2 < deleteLen)) {
      isRemoved(left) = true
      left -= 1
    }
    while (sanitized.indices.contains(right) && (sanitized(right)._1 || sanitized(right)._2 < deleteLen)) {
      isRemoved(right) = true
      right += 1
    }
  }
  println(count)
  @tailrec def sanitize(file: List[(Boolean, Int)], result: List[(Boolean, Int)] = Nil): Array[(Boolean, Int)] = {
    (file, result) match {
      case (Nil, _) ⇒ result.reverse.toArray
      case ((f@(true, _))::r, Nil) ⇒ sanitize(r, f::result)
      case (_::r, Nil) ⇒ sanitize(r, Nil)
      case ((true, l)::fr, (true, r)::rr) ⇒ sanitize(fr, (true, min(l, r))::rr)
      case ((false, l)::fr, (false, r)::rr) ⇒ sanitize(fr, (false, max(l, r))::rr)
      case (f::fr, _) ⇒ sanitize(fr, f::result)
    }
  }
}