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 Haskell (GHC 7.10.3) (judge verdict: Time Limit Exceeded)

module Main where

import Data.Char (isSpace)
import Data.Maybe (fromJust)
import Data.List (unfoldr, nub)
import qualified Data.ByteString.Char8 as BS


main :: IO ()
main = do
  [k,s] <- readIntList . head <$> plainInputs
  print $ length $ nub $ filter (bounds s) $ tiles k s
  where
    bounds s (x,y,z) = x + y + z == s

tiles :: Int -> Int -> [(Int,Int,Int)]
tiles k s = do
  x <- [0..lim s]
  y <- [0..lim (s-x)]
  pure (x,y,min (s-x-y) k)
  where
    lim a = min a k


-- utils
readInt :: BS.ByteString -> Int
readInt = fst . fromJust . BS.readInt

readIntList :: BS.ByteString -> [Int]
readIntList = unfoldr (BS.readInt . BS.dropWhile isSpace)

plainInputs :: IO [BS.ByteString]
plainInputs = BS.lines <$> BS.getContents