// シンプルにする2つ目
// 高階関数を利用
// これはまだ早いかもなぁ

fun main (args:Array<String>){
    val (a , b , c , k) = readLine()!!.split(" ").map(String::toLong)

    val sumCount = fun(lastSelectCount: Long, sumNum: Long): (cardCount: Long, cardNum: Long) -> Long {
        var lastSelectCount = lastSelectCount
        var sumNum = sumNum
        return fun(cardCount: Long, cardNum: Long): Long {
            if (cardCount < lastSelectCount) {
                lastSelectCount -= cardCount
                sumNum += cardCount * cardNum
            } else {
                sumNum += lastSelectCount * cardNum
                lastSelectCount = 0
            }
            return sumNum
        }
    }

    val calc = sumCount(k, 0)
    calc(a, 1)
    calc(b, 0)
    print(calc(c, -1))
}
