import Control.Monad.ST
import Data.Char
 
 
readInt :: IO Int
readInt = read <$> getLine :: IO Int
 
readIntSplit :: IO [Int]
readIntSplit = map read . words <$> getLine :: IO [Int]
 
readStr :: IO String
readStr = getLine
 
readStrSplit :: IO [String]
readStrSplit = map read . words <$> getLine

solve :: Int -> Int -> Int -> (Int, Int)
solve value x k
  | value <= k = (x * value, k - value)
  | k > 0 = (x * k, 0)
  | otherwise = (0, 0)

main = do
  [a, b, c, k] <- readIntSplit
  let (ans1, k1) = solve a 1 k
  let (_, k2) = solve b 0 k1
  let (ans3, _) = solve c (-1) k2
  print $ ans1 + ans3
