Problem:
Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000

Solution in Ruby (2.7.1) (judge verdict: Accepted)

A, B, C, K = gets.split(" ").map!{|i| i.to_i}
=begin
puts "A：" + A.to_s
puts "B：" + B.to_s
puts "C：" + C.to_s
puts "K：" + K.to_s
=end

if K >= A
  k_rest =  K - A
  a_used = A
  #puts "A使用枚数：" + a_used.to_s #Aを使った枚数
  #puts "残りK枚数：" + k_rest.to_s #残るKの枚数
  a_point = a_used.to_i * 1
  #puts "A使用時のポイント：" + a_point.to_s
else
  k_rest = 0
  a_used = K
  #puts "A使用枚数：" + a_used.to_s #Aを使った枚数
  #puts "残りK枚数：" + k_rest.to_s #残るKの枚数
  a_point = a_used.to_i * 1
  #puts "A使用時のポイント：" + a_point.to_s
end

if k_rest >= 1
  if k_rest >= B
    k_rest =  k_rest - B
    b_used = B
    #puts "B使用枚数：" + b_used.to_s #Bを使った枚数
    #puts "残りK枚数：" + k_rest.to_s #残るKの枚数
    b_point = b_used.to_i * 0
    #puts "B使用時のポイント：" + b_point.to_s
  else
    b_used = k_rest
    k_rest = 0
    #puts "B使用枚数：" + b_used.to_s #Bを使った枚数
    #puts "残りK枚数：" + k_rest.to_s #残るKの枚数
    b_point = b_used.to_i * 1
    #puts "B使用時のポイント：" + b_point.to_s
  end
end

if k_rest >= 1
  if k_rest >= C
    k_rest =  k_rest - C
    c_used = C
    #puts "C使用枚数：" + c_used.to_s #Cを使った枚数
    #puts "残りK枚数：" + k_rest.to_s #残るKの枚数
    c_point = c_used.to_i * -1
    #puts "C使用時のポイント：" + c_point.to_s
  else
    c_used = k_rest
    k_rest = 0
    #puts "C使用枚数：" + c_used.to_s #Cを使った枚数
    #puts "残りK枚数：" + k_rest.to_s #残るKの枚数
    c_point = c_used.to_i * -1
    #puts "C使用時のポイント：" + c_point.to_s
  end
end

ans = a_point.to_i + b_point.to_i + c_point.to_i
puts ans