Problem:
Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

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

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.

Solution in Scala (2.11.7) (judge verdict: Time Limit Exceeded)

object ABC051B {

  import Competitive._

  val K, S = nextInt
  println(Range.inclusive(0, K).flatMap(i => Range.inclusive(0, K).map(j => (i, j, S - (i + j)))).count(t => t._3 >= 0 && K >= t._3))
}
object Main extends App{
  ABC051B
}

object Competitive{
  import scala.io.StdIn.readLine

  implicit class ForInt(val self: Int) {
    @inline def fori(from: Int)(body: Int => Unit): Unit = {
      var i = from
      while (i < self) {
        body(i)
        i += 1
      }
    }

    @inline def fori()(body: Int => Unit): Unit = fori(0)(body)

    @inline def times(body: => Unit): Unit = fori(0){_ => body}
  }

  private var tokenizerBack = readLine().split(" ").iterator
  def tokenizer: Iterator[String] = {
    tokenizerBack = if (tokenizerBack.hasNext) tokenizerBack else readLine().split(" ").iterator
    tokenizerBack
  }

  def next: String = tokenizer.next
  def nextInt: Int = next.toInt
  def nextLong: Long = next.toLong
  def nextIntArray(n: Int): Array[Int] = Array.fill(n)(nextInt)
  def nextLongArray(n: Int): Array[Long] = Array.fill(n)(nextLong)
  def nextArray(n: Int): Array[String] = Array.fill(n)(next)
}