Problem:
Score : 100 points

Problem Statement

In K-city, there are n streets running east-west, and m streets running north-south. Each street running east-west and each street running north-south cross each other. We will call the smallest area that is surrounded by four streets a block. How many blocks there are in K-city?

Constraints

2 ≤ n, m ≤ 100

Input

Input is given from Standard Input in the following format:

n m

Output

Print the number of blocks in K-city.

Sample Input 1

3 4

Sample Output 1

6

There are six blocks, as shown below:

Sample Input 2

2 2

Sample Output 2

1

There are one block, as shown below:

Solution in Scala (2.11.7) (judge verdict: Runtime Error)

import scala.annotation.tailrec

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new java.util.Scanner(System.in)
    val h, w = sc.nextInt
    val n = sc.nextInt
    val as = Vector.fill(n)(sc.nextInt)

    val rows = split(go(1, as, Vector[Vector[Int]]()).flatten, w)
    val ans = reverseOdd(rows)

    ans.foreach{ a =>
      a.foreach { b =>
        printf("%s ", b)
      }
      println()
    }

  }

  @tailrec
  def go(i: Int, seq: Seq[Int], result: Seq[Seq[Int]]): Seq[Seq[Int]] = {
    if (seq.length == 0) result
    else go(i+1, seq.tail, result :+ Seq.fill(i)(seq.head))
  }

  def split(seq: Seq[Int], n: Int): Seq[Seq[Int]] = {
    def go(s: Seq[Int], result: Seq[Seq[Int]]): Seq[Seq[Int]] = {
      if (s.length == 0) result
      else go(s.drop(n), result :+ s.dropRight(s.length - n))
    }
    go(seq, Seq[Seq[Int]]())
  }

  def reverseOdd(seq: Seq[Seq[Int]]): Seq[Seq[Int]] = {
    def go(i: Int, s: Seq[Seq[Int]], result: Seq[Seq[Int]]): Seq[Seq[Int]] = {
      if (s.length == 0) result
      else if (i % 2 == 0) go(i+1, s.tail, result :+ s.head)
      else go(i+1, s.tail, result :+ s.head.reverse)
    }
    go(0, seq, Seq[Seq[Int]]())
  }
}