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 0 s and C cards with -1 s.  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 1 s 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 (Python):
a, b, c ,k = map(int, input().split())
fromA = a if k > a else k
fromB = 0
fromC = 0
k = k - fromA
if k > 0:
    fromB = b if k > b else k
    k = k - fromB
if k > 0:
    fromC = c if k > c else k
    k = k - fromC
total = (fromA * 1) + (fromB * 0) + (fromC * -1)
print(total)

Solution (Python):
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))

t = inpl()
a = t[0]
b = t[1]
c = t[2]
k = t[3]
cnt = 0

if k == a + b + c:
    cnt = a - c

if k >= a + b and k < a + b + c:
    cnt = a - (k - (a + b))

if k >= a and k < a + b:
    cnt = a

if k < a:
    cnt = k

print(cnt)

Solution (Python):
a, b, c, k = map(int, input().split())

if k <= a:
    print(k)
elif k <= a+b:
    print(a)
else:
    c_num = k - a - b
    total = a - c_num
    print(total)

Solution (Python):
a=input().split()
pl1=int(a[0])
pl0=int(a[1])
pl_1=int(a[2])
draw=int(a[3])

if(pl1>=draw):
  print(draw)
elif(pl1+pl0>=draw):
  print(pl1)
else:
  
  
  draw_pl_1=draw-pl1-pl0
  
  print(pl1-draw_pl_1)

Solution (Python):
A,B,C,K = map(int, input().split())
now = K
sum = 0

if A < K:
  now -= A
  sum += A
  if B < now:
    now -= B
    print(sum - now)
  else:
    print(sum)
else:
  print(K)

Solution (Python):
A, B, C, K = map(int, input().split())
if A >= K:
  print(K)
elif A + B >= K:
  print(A)
else:
  print(2*A+B-K)

Solution (Python):
(A, B, C, K) = list(map(int, input().split()))
items = [
    (A, 1),
    (B, 0),
    (C, -1)
]

_sum = 0
k = K
for num_cards, val in items:
    if num_cards <= k:
        num = num_cards
        k -= num_cards
    else:
        num = k
        k = 0
    _sum += (val * num)
    if k == 0:
        break

print(_sum)

Solution (Python):
a,b,c,k=map(int,input().split())
if k<=a:
    print(1*k)
else:
    if k-a<=b:
        print(1*a)
    else:
        print(1*a-1*(k-a-b))

Solution (Python):
A, B, C, K = map(int, input().split())
ans = 0
if A >= K:
    print(K)
    exit(0)
else:
    K -= A
    ans += A
    if B >= K:
        print(ans)
        exit(0)
    else:
        K -= B
        ans -= K
        print(ans)

Solution (Python):
A, B, C, K = map(int, input().split())

if A >= K:
    print(K)
elif A < K and A+B >= K:
    print(A)
else:
    print(A-(K-A-B))

Solution (Python):
a, b, c, k = map(int, input().split())
total = 0
if a > k:
    total += k
else:
    total += a
    k -= a
    if k - b > 0:
        k -= b
        total -= k
print(total)

Solution (Python):
input_line = input().split()
a = int(input_line[0])
b = int(input_line[1])
c = int(input_line[2])
k = int(input_line[3])

def return_answer(a,b,c,k):
    answer = 0
    if a < k:
        answer += a
        count = k - a

        if b < k:
            count -= b
            answer -= count

    elif a >= k:
        answer = k

    return answer

print(return_answer(a,b,c,k))

Solution (Python):
A, B, C, K = map(int, input().split())

if A >= K:
    nmax = K
elif B >= K-A:
    nmax = A
else:
    nmax = A-(K-A-B)
            
print(nmax)

Solution (Python):
A, B, C, K = map(int, input().split())
sub = A+B-K
if A>=K:
  print(K)
elif sub>=0:
  print(A)
else:
  print(A+sub)

Solution (Python):
a, b, c, k = map(int, input().split())
ans = 0
if k <= a:
    print(k)
else:
    k -= a
    ans += a
    if k <= b:
        print(ans)
    else:
        k -= b
        ans += -1 * k
        print(ans)

Solution (Python):
def q2():
    A, B, C, K = (int(i) for i in input().split())
    ans = 0

    if K <= A:
        ans += K
        return ans
    else:
        K -= A
        ans += A

    
    if K <= B:
        return ans
    else:
        K -= B

    
    if K <= C:
        ans -= K
        return ans
    else:
        ans -= C
        return ans


if __name__ == '__main__':
    print(q2())

Solution (Python):
A, B, C, K = map(int,input().split())
if K <= A:
  print(K)
elif A <  K <= A + B:
  print(A)
else:
  print(2*A + B - K)

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    a,b,c,k=map(int,readline().split())
    if a+b>=k:
        print(min(a,k))
    elif a+b<k:
        n=k-(a+b)
        print(a-n)
if __name__=="__main__":
    main()

Solution (Python):
def main():
	[A,B,C,K] = list(map(int, input().split()))
	if K >= A:
		K = K - A
		ans = A
	else:
		ans = K
		return ans

	if K >= B:
		K = K - B
	else:
		ans = A
		return ans

	if K > C:
		return "NG"
	else:
		return ans - K


     
if __name__ == '__main__':
	print(main())

Solution (Python):
a, b, c, k = map(int, input().split())

if a >= k:
    print(k)
elif a + b >= k:
    print(a)
else:
    print(2*a + b - k)

Solution (Python):
A, B, C, K = map(int, input().split())

if A >= K:
  print(K)
elif A + B >= K:
  print(A)
else:
  d = K - A - B
  print(A-d)

Solution (Python):
A,B,C,K = map(int,input().split())
answer = 0
if K <= A:
    print(K)
    K = 0
elif K > A:
    answer = A
    K = K - A
if K <= B and K > 0 :
    print(answer)
    K = 0
elif K > B:
    K = K - B
if K <= C and K > 0:
    answer = answer - K
    print(answer)

Solution (Python):

input_line = input()
input_line = input_line.split(" ")
A = int(input_line[0])
B = int(input_line[1])
C = int(input_line[2])
K = int(input_line[3])


M = 0

if A > K:
    M = K
elif (A+B) > K:
    M = K
else:
    x = K - (A+B)
    M = A - x


print(M)

Solution (Python):
A,B,C,K = map(int,input().split())

cnt = 0

if K >= A:
    K = K - A
    cnt += A
else:
    cnt = K
    K = -1

if K > 0:
    K = K - B
if K > 0:
    cnt -= K
        
print(cnt)

Solution (Python):
a, b, c, k = map(int, input().split())

if k <= a:
    print(k)
elif a < k <= a + b:
    print(a)
else:
    print(a - (k - a - b))

Solution (Python):
a, b, c, k = map(int, input().split())

if a >= k:
    print(k)
    exit()
else:
    k -= a
    k -= b
    if k > 0:
        print(a - k)
    else:
        print(a)

Solution (Python):
a,b,c,k=map(int,input().split())
if a > k:
    ans=k
elif a == k:
    ans=k
elif a+b>=k:
    ans=a
else:
    j=k-a-b
    ans=a-j
print(ans)

Solution (Python):
A,B,C,K = map(int, input().split())
ans =0
for c,i in enumerate([A,B, C]):
  K -= i
  if K>=0:
    if c ==0:
    	ans += i
    elif c == 2:
      	ans -= i
      
  else:
    if c ==0:
    	ans += K +i
    elif c == 2:
      	ans -= K+i
    break
    
  

print(ans)

Solution (Python):
a, b, c, k = map(int, input().split())

if a - k >= 0:
  print(1*k)
  exit()
else:
  if b -( a + k) >=0:
    print(a)
    exit()
  else:
    print(a-(k-a-b))

Solution (Python):
import sys

A,B,C,K = map(int, input().split())

if A >= K:
    print(K)
    sys.exit()

if B >= (K-A):
    print(A)
    sys.exit()

wa = A
sa = K-A-B
print(wa-sa)

Solution (Python):
a,b,c,k = list(map(int,input().split()))
ans = 0
if a>=k:
    ans=k
else:
    ans+=a
    k-=a
    if b<k:
        k-=b
        ans-=k
print(ans)

Solution (Python):
a, b, c, k = map(int, input().split())

ans = a
if k < a:
    ans = k
    
if k > a + b:
    ans = ans - (k - (a+b))

print(ans)

Solution (Python):
a, b, c, k = map(int, input().split())
x = k-(a+b)
print(k if k<=a else a if k<=a+b else a-x)

Solution (Python):
A,B,C,K=map(int,input().split())

if A>=K:
    print(K)
else:
    x=K-A
    if x<=B:
        print(A)
    else:
        y=K-A-B
        print(A-y)

Solution (Python):
import sys

A,B,C,K = map(int, input().split())

if A >= K:
    print(K)
    sys.exit()

if B >= (K-A):
    print(A)
    sys.exit()

wa = A
sa = K-A-B
print(wa-sa)

Solution (Python):
a,b,c,k= map(int, input().split())

if k<= a:
    print(k)
elif k <= a+b:
    print(a)
else:
    print(2*a+b-k)

Solution (Python):
























A, B, C, K = [int(i) for i in input().split()]

res = 0
if (A >= K):
    print(K)
    quit()
elif (A < K):
    K -= A
    res += A

if (B >= K):
    print(res)
    quit()
elif (B < K):
    K -= B

if (C >= K):
    print(res-K)
    quit()

Solution (Python):
card = list(map(int, input().split()))

if card[3] > card[0] + card[1]:
    x = card[3] - card[0] - card[1]
    print(card[0] + x * -1)
else:
    if card[0] <= card[3]:
        print(card[0])
    else:
        print(card[3])

Solution (Python):
A,B,C,K = map(int,input().split(' '))

if K <= A:
    print(K)
elif A < K <= A + B:
    print(A)
else:
    print(2*A + B - K)

Solution (Python):
arr = list(input().split())
A=int(arr[0])
B=int(arr[1])
C=int(arr[2])
K=int(arr[3])


sum=0


if K > A :
	sum+=A
else:
	sum+=K
K-=A

if K > 0:

	K-=B
	

	if K > 0:
		sum-=K
		

print(sum)

Solution (Python):
a, b, c, k = map(int, input().split())

ans = 0
if a - k >= 0:
    ans = k
else:
    ans += a
    k -= a
    if b - k < 0:
        k -= b

        if c - k >= 0:
            ans -= k
        else:
            ans -= c

print(ans)

Solution (Python):
a, b, c, k = map(int, input().split())

total=0
total+=a

if k - a == 0:
  print(total)
elif k - a < 0:
  print(k)
else:
  k-=a
  if k - b <= 0:
    print(total)
  else:
    k-=b
    print(total-k)

Solution (Python):
A, B, C, K = (int(i) for i in input().split())
if K <= A:
    print(K)
elif K <= (A+B):
    print(A)
else:
    print(A-(K-(A+B)))

Solution (Python):
a,b,c,k = map(int,input().split())
count = k

if (a > k):
    a = k
    b = 0
    c = 0
elif (a + b > k):
    c = 0
    b = k - a 
elif a + b + c > k:
    c = k - (a + b)

print(a*1 + b*0 + c*(-1))

Solution (Python):
a, b, c, k = map(int, input().split())

if a >= k:
    print(k)
elif a + b >= k:
    print(a)
else:
    print(2 * a + b - k)

Solution (Python):
a,b,c,k=input().split()
if int(k)<=int(a):
    print(k)
elif int(k)<=int(a)+int(b):
    print(a)
else:
    print(2*int(a)+int(b)-int(k))

Solution (Python):
a, b, c, k = map(int, input().split())

sum = 0

import sys
if a < k:
  sum = 1 * a
  k = k - a
else:
  print(1 * k)
  sys.exit()
  
if b < k:
  k = k - b
else:
  print(sum)
  sys.exit()
  
print(sum - k)

Solution (Python):

A, B, C, K = map(int, input().split())
ans = 0

if K >= A:
    ans += 1*A
    K -= A
else:
    print(K)
    exit()

if K >= 0:
    ans += K*(B*0)
    K -= B

if K >= 0:
    ans += (K*-1)
    K -= C

if 0 >= K:
    print(ans)

Solution (Python):
A,B,C,K = map(int,input().split())

if A >= K:
    print(K)
elif A + B >= K:
    print(A)
else:
    print(A - (K-(A+B)))

Solution (Python):
a, b, c, k =map(int, input().split())
if k <a:
  print(k)
elif k<=a+b:
  print(a)
else:
  print(a-(k-(a+b)))

Solution (Python):
A, B, C, K = map(int, input().split())

sum = 0

if K - A > 0:
    sum += A
    K -= A
    if K - B > 0:
        K -= B
        if K - C > 0:
            sum = sum - C
        else:
            sum = sum - K
    else:
        pass
else:
    sum += K

print(sum)

Solution (Python):
a, b, c, k = map(int, input().split())
if k == a or k < a or k < a+b:
    print(k)
elif k > a+b:
    print(a-(k-a-b))
elif k == a+b:
    print(a)

Solution (Python):
A, B, C, K = map(int, input().split())
ans = 0
if A > 0:
    ans += min(A, K)
    K = max(0, K - A)
if B > 0:
    K = max(0, K - B)
if C > 0:
    ans -= min(C, K)
print(ans)

Solution (Python):
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))

t = inpl()
a = t[0]
b = t[1]
c = t[2]
k = t[3]
cnt = 0

if k == a + b + c:
    cnt = a - c

if k >= a + b and k < a + b + c:
    cnt = a - (k - (a + b))

if k >= a and k < a + b:
    cnt = a

if k < a:
    cnt = k

print(cnt)

Solution (Python):
A,B,C,K = map(int,input().split())
if A>=K:
    print(K)
elif A+B>=K:
    print(A)
else:
    S = K-(A+B)
    print(A-S)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    a,b,c,k = inpm()
    ans = 0
    ans += min(a,k)
    ans -= max(0,k-a-b)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
a, b, c, k = map(int, input().split())

sum1 = 0
sum2 = 0
sum3 = 0

if k < a: sum = k
if k >= a and k < a+b: sum = a
if k >= a+b: sum = a-(k-a-b)

print(sum)

Solution (Python):
A,B,C,K = map(int, input().split())
limit = K
count = 0

while limit > 0:
    
    if 0 < A and limit <= A:
        count += 1 * limit
        limit = 0
        A -= limit
        break
    
    elif 0 < A:
        count += 1 * A
        limit -= A
        A = 0
    
    if 0 < B and limit <= B:
        count += 0 * limit
        limit = 0
        B -= limit
        break
    elif 0 < B:
        count += 0 * B
        limit -= B
        B = 0
    
    if 0 < C and limit <= C:
        count += -1 * limit
        limit = 0
        C -= limit
        break
    elif 0 < C:
        count += -1 * C
        limit -= C
        C = 0

print(count)

Solution (Python):
ans = 0
a,b,c,k = map(int,input().split())
if a >= k:
    print(k)
elif a + b >= k:
    print(a)
else:
    print(a-(k-(a+b)))

Solution (Python):
A,B,C,K= map(int, input().split())
a=0
c=0
if A<=K:
    a=A
if A>K:
    a=K
if K-A-B>0:
    if C<=K-A-B:
        c=C*-1
    if C>K-A-B:
        c=(K-A-B)*-1
print(a+c)

Solution (Python):
a, b, c, k = map(int, input().split())
count = 0
ans = 0
if(a >= k):
    print(k)
elif(b >= (k-a)):
    print(a)
else:
    print(2*a + b - k)

Solution (Python):
a,b,c,k=map(int,input().split())
s=0
if a>=k:
    s+=k
    print(s)
else:
    s+=a
    if b>=k-a:
        print(s)
    else:
        r=k-a-b
        s+=-r
        print(s)

Solution (Python):
a,b,c,d=map(int,input().split())
sum=0
if d>a:
    sum=sum+a
    d=d-a
else:
    sum = sum+(d)
    d=0
    print(sum)
    exit()
if d>0:
    if b>=d:
        d=0
        print(sum)
        exit()
    else:
        d=d-b
if d>0:
    if c>=d:
        sum=sum-d
        print(sum)
        d=0
        exit()
    else:
        sum=sum-c
        print(sum)
        exit()

Solution (Python):
a, b, c, k = map(int, input().split())
if a >= k:
    print(1 * k)
else:
    if b >= (k - a):
        print(1 * a + (k - a) * 0)
    else:
        if c >= (k - a - b):
            print(1 * a + 0 * b + (-1) * (k - a - b))
        else:
            print(-1)

Solution (Python):
a,b,c,k = map(int,input().split())
if a >= k:
    print(k)
elif b >= k-a:
    print(a)
else:
    print(a-(k-a-b))

Solution (Python):
list=list(map(int, input().split()))
A=list[0]
B=list[1]
C=list[2]
K=list[3]
if K<=A:
    print(K)
elif K-A<=B:
    print(A)
else:
    print(A-(K-A-B))

Solution (Python):
a , b ,c , k = map(int, input().split())

if a >= k:
    print(k)
elif a+b >= k:
    print(a)
elif a > 0 :
    print( a - (k-a-b))
elif b > 0:
    print(-(k-b))
else:
    print(-k)

Solution (Python):

import sys

a,b,c,k = map(int, input().split())

if k <= a:
    sum = k
    print(sum)
    sys.exit()
else:
    sum = a
    k = k - a
if k <= b:
    print(sum)
    sys.exit()
else:
    k = k - b
    sum = sum - k
    print(sum)

Solution (Python):
A,B,C,K = map(int,input().split())

def abc(A,B,C,K):
    result = 0
    
    K = K - A
    result = A + result
    if K < 0:
        K = K + A
        result -= abs(abs(K)-A)
        return result
    K -= B
    if K < 0:
        return result
    K -= C
    result -= C
    if K < 0:
        K += C
        result += abs(abs(K)-C)
    return result
print(abc(A,B,C,K))

Solution (Python):

x = list(map(int,input().split()))
a = x[0]
b = x[1]
c = x[2]
k = x[3]

if k < a + b:
    print(k)
else :
    print(a-(k-(a+b)))

Solution (Python):
a,b,c,k=map(int,input().split())
count=0
if k>=a:
    count+=(1)*a
    k-=a
    if k>=b and k!=0:
        k-=b
        if k>=c and k!=0:
            count+=(-1)*c
        else:
            count+=(-1)*k  
else:
    count+=k  
print(count)

Solution (Python):
a, b, c, k = map(int, input().split())
if k > a:
    af = k - a
    ab = abs(af-b)
    print(a-ab)
else:
    print(k)

Solution (Python):
A, B, C, K = map(int, input().split())

ans = 0
import sys
while K > 0:
    if A and A >= K:
        ans += K
        break
    elif A and A < K:
        ans += A
        K -= A
        A = 0
    elif B and B >= K:
        break
    elif B and B < K:
        K -= B
        B = 0
    elif C and C >= K:
        ans -= K
        break
    elif C and C < K:
        ans -= C
        break
print(ans)

Solution (Python):
a,b,c,k=map(int,input().split())
if a>k:
    print(k)
else:
    if a+b>k:
        print(a)
    else:
        print(a-(k-a-b))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

a, b, c, k = map(int, readline().split())
if a + b >= k:
    print(min(a, k))
else:
    print(a - (k - a - b))

Solution (Python):
A,B,C,K=list(map(int,input().split()))
nokori=K
maisu1=0
maisu2=0
maisu3=0
while True:
    if A<=nokori:
        maisu1=A
        nokori=nokori-maisu1
    else:
        maisu1=nokori
        break

    if B<=nokori:
        maisu2=B
        nokori=nokori-maisu2
    else:
        maisu2=nokori
        break
    
    maisu3=nokori
    break

print(maisu1-maisu3)

Solution (Python):
A, B, C, K = map(int, input().split())
num = []
ans = 0
if(K>0):
    ans += 1*min(A,K)
    K -= A
if(K>0):
    K -= B
if(K>0):
    ans += -1*min(C,K)

print(ans)

Solution (Python):
a,b,c,k = map(int,input().split())
ans = 0
if a == k:
    ans += k
elif a < k:
    ans += a
    k -= a
else:
    ans += k
    k = 0

if b < k:
    k -= b
else:
    k = 0
if c == k:
    ans += -k
elif c < k:
    ans += -c
else:
    ans += -k
print(ans)

Solution (Python):
a, b, c, k = map(int, input().split())
if k > a:
    af = k - a
    ab = abs(af-b)
    print(a-ab)
else:
    print(k)

Solution (Python):
a, b, c, k = map(int, input().split())

if k <= a:
    ans = k
else:
    ans = a
    k = k - a
    
    if k >= b:
        k = k - b
        ans += k * (-1)
print(ans)

Solution (Python):
a,b,c,k=map(int,(input().split()))

if a >= k:
    print(k)
else:
    if a + b > k:
        print(a)
    else:
        d = k -(a + b)
        print(a-d)

Solution (Python):
from sys import stdin

A, B, C, K = map(int, stdin.readline().rstrip().split())
res = int(0)

def f(p, n, k, res):
    if (k - n) >= 0:
        return k - n, p * n + res
    else:
        return 0, p * k + res


K, res = f(1, A, K, res)
K, res = f(0, B, K, res)
_, res = f(-1, C, K, res)
print(res)

Solution (Python):
A, B, C, K = map(int, input().split())
if A >= K:
    print(K)
elif A+B >= K:
    print(A)
else:
    print(A-(K-A-B))

Solution (Python):
A,B,C,K = (int(i) for i in input().split())

sum = 0

if  A >= K:
    sum = A - (A - K)
    print(sum)
    
elif A < K:
    sum = A
    K = K - A
    
    if B >= K:
        
        print(sum)
        
    elif B < K:
        K = K - B
        sum = sum - (K)
        print(sum)

Solution (Python):
a, b, c, k = map(int, input().split())

if(k<=a):
  print(k)
elif(a<k<=(a+b)):
  print(a)
elif(a+b<k<=(a+b+c)):
  print(a-(k-a-b))

Solution (Python):
a,b,c,k= (int(x) for x in input().split())

if a >= k:
    print(k)
elif a+b >= k:
    print(a)
elif a+b+c >= k:
    print(a - (k-(a+b)))
else:
    print(0)

Solution (Python):
def main():
    a, b, c, k = map(int, input().split())
    if a + b >= k:
        if a > k:
            print(k)
        else:print(a)

    else:
        l = k - (a + b)
        print(a - l)
    

if __name__ == "__main__":
    main()

Solution (Python):
a, b, c, d = map(int, input().split())

if a>=d:
  print(d)
elif a+b>=d:
  print(a)
else:
  print(a-(d-(a+b)))

Solution (Python):
A ,B , C ,K = map(int, input().split())
ans = 0
count = K

ans += min(K,A)
K = K -A
if K >0:
    K = max(0, K-B)
    
if K>0:
    ans= ans - K

print(ans)

Solution (Python):
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    a,b,c,k = I2()
    l = k-a-b
    
    if k <= a:print(k)
    elif a+b >= k:print(a)
    elif l <= c:print(a-l)

Solution (Python):
a, b, c, k = map(int, input().split())

ans = 0

if a  >= k:
    print(k)
elif a + b >= k:
    print(a)
else:
    ans = a - (k-a-b)
    print(ans)

Solution (Python):
a,b,c,k = map(int,input().split())

tmp = k
ans = 0
k -= a
if k <= 0:
    print(tmp)
    exit()
ans += a
k -= b
if k <= 0:
    print(ans)
    exit()
k -= c
if k <= 0:
    print(a - (tmp-(a+b)))
    exit()
ans -= c
print(ans)

Solution (Python):
a, b, c, k = map(int, input().split())
total = min(a, k)
k = k - a
k = k - b
if k>0:
  total = total - min(c, k)
print(total)

Solution (Python):
A,B,C,K = map(int,input().split())
if K<=A:
    print(K)
elif A<K and K<=A+B:
    print(A)
else:
    print(A-(K-A-B))

Solution (Python):
a,b,c,k = map(int,input().split())

if a >= k:
    s = k
elif (a+b) >= k:
    s = a
else:
    s = a - (k-a-b)

print(s)

Solution (Python):
a, b, c, k = input().strip().split()
a, b, c, k = [int(a), int(b),int(c), int(k)]
total = 0
k2 = 0
if a > k:
  total = k
else:
  k2 = k-a
  total = a
if k2 > 0:
  if b > k2:
    k2 = 0
  else:
    k2 = k2 - b
if k2 > 0:
  if c > k2:
    total = total +(k2 * -1)
  else:
    total = total +(c * -1)
print(total)

Solution (Python):
a, b, c, k = map(int, input().split())

ans = 0
if a < k:
    ans = a
else:
    ans = k

minus = k - (a + b)

if minus > 0:
    ans -= minus

print(ans)

Solution (Python):
a,b,c,k=map(int,input().split())

print(min(a,k)-max(0,k-a-b))

Solution (Python):
A,B,C,K =map(int,input().split())

if K<=A:
  print(K)
elif A+B >=K:
  print(A)
else:
  print(A-(K-(A+B)))

Solution (Python):


A, B, C, K = map(int, input().split())

sum_mux = 0

least_A, least_B, least_C = A, B, C
least_K = K

while 0 !=  least_K:
    if 0 != least_A:
        if least_K >= least_A:
            sum_mux += least_A
            least_K -= least_A
            least_A = 0
        else :
            sum_mux += least_K
            least_A -= least_K
            least_K = 0
    elif 0 != least_B:
        if least_K >= least_B:
            sum_mux += 0
            least_K -= least_B
            least_B = 0
        else :
            sum_mux += 0
            least_B -=least_K
            least_K = 0
    else:
        if least_K >= least_C:
            sum_mux -= least_C
            least_K -= least_C
            least_C = 0
        else :
            sum_mux -= least_K
            least_C -=least_K
            least_K = 0

print(sum_mux, flush=True)

Solution (Python):
A,B,C,K = map(int,input().split())
if K > A :
    M = K - A
    L = abs(M-B)
    print(A-L)
else :
    print(K)

Solution (Python):
a,b,c,k = map(int,input().split())

d={}

d[1]=a
d[0]=b
d[-1]=c

ans = 0
cnt=0

cnt+=a
ans+=a
if cnt>k:
    ans-=(cnt-k)
    print(ans)
    exit(0)

cnt+=b
if cnt>k:
    print(ans)
    exit(0)

cnt+=c
ans-=c
if cnt>k:
    ans+=(cnt-k)
    print(ans)
    exit(0)

if cnt==k:
    print(ans)

Solution (Python):
A, B, C, K = map(int, input().split())

if A >= K:
    print(K)
elif A < K and A + B >= K:
    print(A)
else:
    print(A - (K - A - B))

Solution (Python):
A,B,C,K = input().split()
A = int(A)
B = int(B)
C = int(C)
K = int(K)

output = 0

while True:
    if K == 0:
        break
    if A > 0:
        if K >= A:
            output = 1*A
            K -= A
            A = 0
            continue
        else:
            output = 1*K
            break
    if B > 0:
        if K >= B:
            K -= B
            B = 0
            continue
        else:
            break
    output -= 1*K
    K = 0

print(output)

Solution (Python):
A,B,C,K = map(int,input().split())
s = 0
if A <= K:
    s += A*1
    K = K-A
else:
    s += K*1
    K = 0
    
if B <= K:
    s += B*0
    K = K-B
else:
    s += K*0
    K = 0
    
if C >= K:
    s += K*(-1)
else:
    s += C*(-1)
    
print(s)

Solution (Python):
a,b,c,k = map(int, input().split())
if a >= k:
    print(k)
elif a + b >= k:
    print(a)

elif a + b < k:
    print(a - (k - (a + b)))

Solution (Python):
a, b, c, k = map(int, input().split())
ans = -10 * 10 ** 10

if a > 0:
    if k > a:
        ans = a
        k -= a
    else:
        print(k)
        exit()

if b > 0:
    ans = max(ans, 0)
    if k > b:
        k -= b
    else:
        print(ans)
        exit()

if c > 0:
    ans = max(ans - k, -k)

print(ans)

Solution (Python):
"""
"""
A, B, C, K = map(int, input().split())
sum = 0

if A >= 1:
    if K - A >= 0:
        sum += A
        K -= A
    else:
        sum += K
        K = 0
if B >= 1 and K >= 1:
    if K - B >= 0:
        K -= B
    else:
        K = 0

if C >= 1 and K >= 1:
    if K - C >= 0:
        sum -= C
        K -= C
    else:
        sum -= K
        K = 0

print(sum)

Solution (Python):
A,B,C,K = map(int,input().split())

Atoru = min(A,K)
Btoru = max(0,min(B,K-A))
Ctoru = max(0,K-A-B)

print(Atoru-Ctoru)

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    A, B, C, K = map(int, input().split())
    rest = K
    ans = 0
    ans += min(A, rest)
    if K > A:
        rest -= A
        if rest > B:
            rest -= B
            ans -= min(rest, C)
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
import sys
a,b,c,k = map(int, input().split())

n_sum = 0
if a >= k:
  n_sum += k
  print(n_sum)
  sys.exit()
else:
  n_sum += a
  k -= a

k -= b

if k <=0:
  print(n_sum)
  sys.exit()
  
if c >= k:
  n_sum -= k
  print(n_sum)
  sys.exit()
else:
  n_sum -= c
  print(n_sum)
  sys.exit()

Solution (Python):
a, b, c, k = map(int,input().split())
cnt = 0
if a < k:
    cnt += a
    k -= a
    k -= b
    if k <= 0:
        print(a)
        exit()
    print(cnt-min(c, k))
    exit()
else:
    print(k)
    exit()

Solution (Python):
A = input().split(" ")
sum=0
for i in range(len(A)):
    A[i]=int(A[i])
for i in range(len(A)-1):
    if A[len(A)-1] >= A[i]:
        sum+=(len(A)-3-i)*A[i]
        A[len(A)-1]= A[len(A)-1]-A[i]
    elif A[len(A)-1] < A[i]:
        sum+=(len(A)-3-i)*A[len(A)-1]
        A[len(A)-1]=0
print(sum)

Solution (Python):
a, b, c, k = [int(x) for x in input().split()]
 
if k <= a:
  print(k)
elif a < k and k<= a+b:
  print(a)
else:
  print(a-(k-a-b))

Solution (Python):
A, B, C, K = map(int, input().split())

if (A >= K):
    print(K)
elif (A + B > K):
    print(A)
else:
    print(A - (K - (A + B)))

Solution (Python):
A,B,C,K=map(int,input().split())

tot=0
all=K

if K-A>=0:
    tot+=A*1
    all-=A
    if all-B>=0:
        tot+=B*0
        all-=B
        if all-C>=0:
            tot+=-1*C
        else:
            tot+=-1*all
    else:
        tot+=0*all
else:
    tot+=1*all
print(tot)

Solution (Python):
S = list(map(int, input().split()))
Sum = 0
if S[0] >= S[3]:
  Sum = S[3]
elif S[0] + S[1] >= S[3]:
  Sum = S[0]
else:
  Sum = S[0]*2 - S[3] + S[1]
print(Sum)

Solution (Python):
a = list(map(int,input().split()))
if a[0] >= a[3]:
  print(a[3])
else:
  a[3] = a[3] -a[0]
  if a[1] >= a[3]:
    print(a[0])
  else:
    print(a[0]-a[3]+a[1])

Solution (Python):
plus, zero, minus, number = map(int, input().split(" "))

if(plus > number):
  print(number)
elif(plus + zero >= number):
  print(plus)
else:
  print(plus - number + plus + zero)

Solution (Python):
a, b, c, k = map(int, input().split())
if a >= k :
    print(k)
elif b >= (k-a):
    print(a)
else:
    print(a-k+a+b)

Solution (Python):
a, b, c, k = map(int, input().split())
if a > k:
    print(k)
elif k >= a and b > k-a:
    print(a)
else:
    d = k - (a+b)
    print(a-d)

Solution (Python):
A, B, C, K = map(int, input().split())
lst = []
sum = 0
if A <= K:
    sum += 1 * A
else:
    sum += 1 * K

k = K - (A + B)

if (C <= k) and (k > 0):
    sum += -1 * C
elif (C > k) and k > 0:
    sum += -1 * k

print(sum)

Solution (Python):
a,b,c,k=map(int, input().split())
t=max(0,k-a)
if k<a+b:
        print(k-t)
else:
        print(a-(k-a-b))

Solution (Python):
A, B, C, K = map(int,input().split())
if K <= A+B:
  print(min(K , A))
else:
  print(A-(K-(A+B)))

Solution (Python):
a,b,c,k= map(int, input().split())
tmp=0
if k <=a:
	print(k)
elif k<=a+b:
	print(a)
else:
	tmp=k-a-b
	print(a-tmp)

Solution (Python):
A,B,C,K = map(int,input().split())



if A >= K:
    print(K)
elif A < K and K - B - A <= 0:
    print(A)
else:
    print(A - (K - A - B))

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

a,b,c,k = iim()
if k < a:
    print(k)
elif k <= a+b:
    print(a)
else:
    print(a+(k-a-b)*(-1))

Solution (Python):




input_1 = input()
a = int(input_1.split(" ")[0])
b = int(input_1.split(" ")[1])
c = int(input_1.split(" ")[2])
k = int(input_1.split(" ")[3])

max_value = 0

if k <= a:
    max_value = k
elif k <= b:
    max_value = a
else:
    max_value = a - (k - a - b)

print(max_value)

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  

A, B, C, K = read_ints()

if A >= K:
    print(K)
elif A + B >= K:
    print(A)
else:
    print(A - (K - (A + B)))

Solution (Python):
A,B,C,num = map(int, input().split())
if num <= A+B:
    if num <= A:
        print(num)
    else:
        print(A)
else:
    ans = A-(num-A-B)
    print(ans)