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())

ans=0
if A<=K:
    K-=A
    ans=A
    if B<=K:
        K-=B
        if C<=K:
            ans-=C
        else:
            ans-=K
    else:
        B=K-B
else:
    ans=K
print(ans)

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

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

Solution (Python):

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)

def acinput():
    return list(map(int, input().split(" ")))

A,B,C,K=acinput()


nA=min(K,A)
nB=min(K-nA,B)
nC=min(K-nA-nB,C)

print(nA-nC)

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

if K <= A:
    print(K)
elif K - A - B <= 0:
    print(A)
elif K - A - B - C <= 0:
    print(A + (K - A - B) * -1)

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

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

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

print(S)

Solution (Python):
L = list(map(int, input().split()))
n = [1, 0, -1]
maxres = 0

for i in range(3):
    if L[3] == 0:
        break
    maxres += L[i] * n[i]
    L[3] -= L[i]
    
    if L[3] < 0:
        maxres -= abs(L[3]) * n[i]
        break
print(maxres)

Solution (Python):
a = input().rstrip().split(' ')
A,B,C,K = int(a[0]),int(a[1]),int(a[2]),int(a[3])
s = 0

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

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

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

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

if C >= K:
    print(ans - K)
else:
    print(ans - C)

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

if a>=k:
  ans=1*k
  
elif a+b>=k:
  ans=1*a

else:
  ans=a-(k-a-b)
      
print(ans)

Solution (Python):
A, B, C, K = map(int, input().split())
count = 0
if A == K:
    print(A)
    quit()
if K < A:
    print(K)
    quit()
D = K-A
print(A-(D-B))

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

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

ans = 0

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

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

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

sum = 0

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

print(sum)

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

big = 0
if A >= K:
    big = K
else:
    big = A
    K -= A
    
    if B < K:
        K -= B
        
        big -= K

print(big)

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

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

ans = 0

def main():
    global K

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

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

    
    return ans - K

result = main()

print(result)

Solution (Python):
A, B, C, K = map(int, input().split())
count = 0
if A == K:
    print(A)
    quit()
if K < A:
    print(K)
    quit()
D = K-A
print(A-(D-B))

Solution (Python):
def do():
    a,b,c,k=map(int,input().split())
    if a>=k:
        return k
    if k<=a+b:
        return a
    elif k>a+b:
        d=min(c,k-a-b)
        return a-d
print(do())

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

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

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


if A <= K:
    ans += A*1
    K -= A
else:
    ans += K*1
    flg = False

if flg:
    if B <= K:
        ans += B*0
        K -= B
    else:
        ans += K*0
        flg = False

if flg:
    if C <= K:
        ans -= C*1
        K -= C
    else:
        ans -= K*1
        flg = False


print(ans)

Solution (Python):
a = list(map(int, input().split()))
n = a[0]
m = a[1]
l = a[2]
k = a[3]

if 0 < k <= n:
    print(k)

elif n < k <= n + m:
    print(n)

elif n+m <= k < n+m+l:
    print(n -1*(k -n -m))

elif n+m+l <= k:
    print(n - l)
else:
    print(0)

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

while K > 0:
    if A > 0:
        if K > A:
            total = total + A
            K = K - A
            A = 0;
        else:
            total = total + K
            K = 0
            A = A - K
    elif B > 0:
        if K > B:
            K = K - B
            B = 0;
        else:
            K = 0
            B = B - K
    elif C > 0:
        if K > C:
            total = total - C
            K = K - C
            C = 0;
        else:
            total = total - K
            K = 0
            C = C - K

print(total)

Solution (Python):
import sys

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

S = 0

if A >= K:
    S = S + K * 1
    K = 0
    print(S)
    sys.exit()
else:
    S = S + A * 1
    K = K - A

if B >= K:
    K = 0
    print(S)
    sys.exit()
else:
    K = K - B

if C >= K:
    S = S + K * (-1)
    K = 0
    print(S)
    sys.exit()
else:
    S = S + C * 1
    print(S)

Solution (Python):
from sys import stdin


def get_result(data):
    A, B, C, K = data
    if (A + B) >= K:
        return K if A > K else A
    else:
        return A - (K-(A+B))


if __name__ == '__main__':
    data = list(map(int, stdin.readline().rstrip().split(' ')))
    result = get_result(data)
    print(result)

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

if k-a <= 0:
  count = k
elif k-a-b <= 0:
  count = a
else:
  count = 2*a + b - k

print(count)

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

left = k
sum = 0
cards = (a, b, c)

for i, card in enumerate(cards):
  if card <= left:
    sum += card * (1-i)
    left -= card
  elif left > 0:
    sum += left * (1-i)
    left = 0

print(sum)

Solution (Python):
a,b,c,k = (int(x) for x 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())
sum = 0
if a >= k:
    sum = 1*k
else:
    if b >= (k - a):
        sum = (1*a) + (0*(k-a))
    else:
        if c >= (k - a - b):
            sum = (1*a) + (0*(k-a)) + (-1*(k-a-b))
print(sum)

Solution (Python):
def main(A,B,C,K):
    card=K-A
    if card <= 0:
        return K
    card-=B
    if card <= 0:
        return A
    return A-card

if __name__ == "__main__":
    A,B,C,K=map(int,input().split())
    print(main(A,B,C,K))

Solution (Python):

A, B, C, K = [int(x) for x in input().split(" ")]
 
if (K-A) > 0:
    if (K-A-B) > 0:
        print(A-(K-A-B))
    else:
        print(A)
else:
    print(K)

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

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

Solution (Python):
import sys

a, b, c, k = map(int, input().split())
if a >= k:
    print(k)
    sys.exit()

total = a
k = k - a
if k <= 0:
    print(total)
    sys.exit()

k = k - b
if k <= 0:
    print(total)
    sys.exit()

print(total - k)

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

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

if k>=c:
    k=k-c
    s=s+c*(-1)
else:
    
    s=s+k*(-1)
    print(s)
    sys.exit()
    
print(s)

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

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

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

if D > A:
 
    ancer = 1 * A + 0 * (D - A) + (-1 *(D - A - B))
    
else:
    ancer = 1 * D

print(ancer)

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

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

print(ans)

Solution (Python):

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


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

Solution (Python):
import sys











stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18



A, B, C, K = map(int, stdin.readline().rstrip().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())
if K <= A:
    print(K)
elif A+B < K:
    print(A-(K-A-B))
else:
    print(A)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  a,b,c,k=LI()
  ans=0
  if k<a:
    ans=k
  elif k<a+b:
    ans=k
  else:
    ans=a
    ans-=(k-(a+b))
  return ans


print(main())

Solution (Python):
num_list = [int(i) for i in input().split()]

if num_list[3] <= num_list[0]:
    print(1*num_list[3])
elif num_list[3] > num_list[0] and num_list[3] <= num_list[0] + num_list[1]:
    print(1*num_list[0])
else:
    print(1*num_list[0]+(-1*(num_list[3] - (num_list[0]+num_list[1]))))

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-(B+A)))

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

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

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

count = 0
x = 0

if K - A >= 0:
    x = K - A
    count = A * 1
else:
    count = K * 1

if x >= 0:
    x = x - B
    if x >= 0 and C != 0:
        C = K - A - B
        count = count + C * -1

print(count)

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

if a >= k:
  a = k
  b = 0
  c = 0
if a < k and a+b >= k:
  c = 0
if a+b < k:
  c = k - a - b
  
sum_a = 1 * a
sum_c = -1 * c
  
print(sum_a + sum_c)

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

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

if K <= B:
    print(A)
    exit()
else:
    K = K - B
    print(A-K)
    exit()

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

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 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+B):
    sum += (A-(K-A-B))
else:
    sum += K
    
print(sum)

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

if k <= a:
  res += k * 1
  print(res)
else:
  res += a * 1
  k -= a
  if k <= b:
    res += k * 0
    print(res)
  else:
    res += b * 0
    k -= b
    res += k * -1
    print(res)

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

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

else:
    print(k-b-2*(k-a-b))

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 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:
    K=K-A
    if K>B:
        K=K-B
        print(A-K)
    else:
        print(A)
elif K==A:
    print(A)
else:
    print(K)

Solution (Python):
import sys


def test():
    A, B, C, K = map(int, input().split())
    ans = 0
    if A > 0 and (K - A) > 0:
        ans += A
        K -= A
    elif A > 0 and (K - A) <= 0:
        ans += K
        print(ans)
        sys.exit()

    if B > 0 and (K - B) > 0:
        K -= B
    elif B > 0 and (K - B) <= 0:
        print(ans)
        sys.exit()

    ans -= K
    print(ans)


if __name__ == "__main__":
    test()

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

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

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

n_a = 0
n_b = 0
n_c = 0

if K > A:
  n_a = A

  if K-A > B:
    n_b = B
    n_c = K-A-B
  
  if K-A == B:
    n_b = B
    n_c = 0
  
  if K-A < B:
    n_b = K-A
    n_c = 0 

if K == A:
  n_a = K
if K < A:
  n_a = K

print(n_a+(-1)*n_c)

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

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

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

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

Solution (Python):
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

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

print(ans)

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):



                            
                            
                            
                            



import sys
import math as mt


def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))

def printwsp(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")
def printchk(*args): return print(*args, end="tst, ")

MOD = int(1e9+7); BABYMOD = 998244353;



a, b, c, k = get_ints()
ans = 0
ans += min(a, k)
k = k - ans
k = max(0, k-b)
ans -= min(k, c)
print(ans)


'''
THE LOGIC AND APPROACH IS BY ME @luctivud.
SOME PARTS OF THE CODE HAS BEEN TAKEN FROM WEBSITES LIKE::
(I Own the code if no link is provided here or I may have missed mentioning it)
: DO NOT PLAGIARISE.
'''

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()) 

if k <= a:
  print(k)
else:
  k1=k-a
  if k1 <=b:
    print(a)
  else:
    k2=k1-b
    print(a-k2)

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

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

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

k2 = k1-b
if k2<=0:
    print(ans)
    sys.exit()
else:
    ans=ans

print(ans-k2)

Solution (Python):
A,B,C,K = map(int,input().split())
dif = K - (A + B)
ans = 1 * A

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

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

k -= A
if k < 0:
    k = 0
    result += 1 * K
else:
    result += 1 * ( A )

    k -= B
    if k > 0:

        result -= 1 * ( k )

print(result)

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 = input().split()
a = int(a)
b = int(b)
c = int(c)
k = int(k)






if a < 0:
    a = 0

if b < 0:
    b = 0

if c < 0:
    c = 0

max_val = 0

x = k - a

if x >= 0:
    max_val = max_val + a * 1
    k = x
elif x < 0:
    max_val = max_val + k * 1
    k = 0


if k > 0:
    x = k - b
    if x >= 0:
        max_val = max_val + b * 0
        k = x
    elif x < 0:
        k = 0
        max_val = max_val + k * 0


if k > 0:
    x = k - c
    if x >= 0:
        max_val = max_val + c * -1
    elif x < 0:
        max_val = max_val + k * -1

print(max_val)

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

A = 0
if (a<d):
  if (0 < (d-a-b)):
    A = a-(d-a-b)
  else:
  	A = a
else:
  A = d
print(A)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")


def solve():
    a, b, c, k = LI()
    if k < a:
        print(k)
    else:
        k -= a
        if k < b:
            print(a)
        else:
            k -= b
            print(a- k)
    return



if __name__ == '__main__':
    solve()

Solution (Python):
A, B, C, K = map(int, input().split())
P = 0
S = 0
while S==0:
    P += A
    K -= A
    if K < 0:
        P -= abs(K)
        break
    elif K == 0:
        break

    K -= B
    if K <= 0:
        break

    P -= C
    K -= C
    if K <= 0:
        P += abs(K)
        break
    S=1
print(P)

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

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

Solution (Python):

if __name__ == "__main__":
    A, B, C, K = map(int, input().split())
    cnt = 0
    if K >= A:
        cnt += A * 1
        K -= A
        if K >= B:
            K -= B
            if K >= C:
                cnt += C * -1 
            else:
                cnt += K * -1
        else:
            K -= K
    else:
        cnt += K * 1
        K -= 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: print(a - (k - (a + b)))

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














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

Solution (Python):
A, B, C, K = [int(n) for n in input().split()]
c = 0
k = K
c += min(A, K)
K -= min(A, K)
if K==0:
    print(c)
else:
    K -= B
    if K==0:
        print(c)
    else:
        c -= K
        print(c)

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


def function(a,b,c,k):
    count =0
    if a>=k:
        return k
    else:
        count =a
        k =k-a
        a=0
        if b>=k:
            return count
        else:
            k-=b
            b=0
            count-=k

        return count
print(function(a,b,c,k))

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

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

if k - a  > 0:
    k = k - a
    ans = ans + (a * 1)
    if k - b > 0:
        k = k - b
        ans = ans + (b * 0)
        if k - c > 0:
            k = k - c
            ans = ans + (c * (-1))
        elif c - k >= 0:
            ans = ans + (k * (-1))
        else:
            ans
    elif b - k >= 0:
        ans = ans + (k * (0))
    else:
        ans
elif a - k >= 0:
    ans = ans + (k * (1))       
else:
    ans

print(ans)

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

ans=0

if K<=A:
    ans=K
    
else:
    if K-A<=B:
        ans=A
    else:
        ans=A-(K-A-B)
        
print(ans)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from bisect import bisect_left, bisect_right, insort
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def MS(): return map(str,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())



a, b, c, k = MI()

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())
score = 0

if a <= k:
    score += a
    k -= a
else:
    score += k
    k = 0

if b < k:
    k -= b
    score -= k

print(score)

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

if K <= A :
   ans = K
   print(ans)

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

   elif M >= B :
      L = M - B
      ans = A - L
      print(ans)

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

for i in range(1,4):
    if i == 1:


        if A <= K:
            ans += A
            K -= A
        
        else:
            ans += K
            break
        
        
    elif i == 2:
        
        
        if B <= K:
            K -= B
        
        else:
            K = 0
            break
        

    elif i == 3:
        
        
        if C <= K:
            ans -= C
        
        else:
            ans -= K
        


print(ans)

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


ka = k - a

if ka > 0:
    kb = ka - b
elif ka <= 0:
    a = k
    ka = 0
    kb = 0
else:
    kb = 0
print(a*1+ka*0+kb*-1)

Solution (Python):
def main():
    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)
if __name__ == '__main__':
    main()

Solution (Python):
A,B,C,K=map(int,input().strip().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 = list(map(int,input().split()))

cardList = [A,B,C]
numList = [0,0,0]
Hand = K
count = 0

def check(card):
  global Hand
  if Hand - card <= 0:
    return Hand
  else:
    return card

for card in cardList:
  numList[count] = (check(card))
  Hand = Hand - card
  count += 1
  if Hand <= 0:
    break;

print(numList[0] * 1 + numList[1] * 0 + numList[2] * -1)

Solution (Python):

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

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

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

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

Solution (Python):

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

ans = 0

if K <= A:
    ans = K

elif K <= A + B:
    ans = A

else:
    ans = A - (K - A - B)
    

print(ans)

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

if not a==0:
    if a<k:
        res+=a
        k-=a
    elif a==k:
        print(a)
        exit()
    else:
        print(k)
        exit()
if not b==0:
    if b<k:
        k-=b
    elif b==k:
        print(res)
        exit()
    else:
        print(res)
        exit()
if not c==0:
    if c<k:
        print(res-c)
        exit()
    elif c==0:
        print(res-c)
        exit()
    else:
        print(res-k)
        exit()
else:
    print(res)
    exit()

Solution (Python):
m = list(map(int, input().split()))
count = 0
k = m[3]
if m[0] < k:
    count = m[0]
    k = k - m[0]
    if m[1] < k:
        k = k - m[1]
        if m[2] < k:
            count -= m[2]
        else:
            count -= k
    else:
        count
else:
    count = k
print(count)

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

Solution (Python):
import sys
input = sys.stdin.readline
import numpy as np

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

if A < K:
    ans += A
    K -= A
else:
    ans += K
    K -= K
    
if (K != 0) and (B < K):
    K -= B
else:
    K -= K

if (K != 0) and (C < K):
    K -= C
    K -= C
else:
    ans -= K
    K -= K

print(ans)

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

score = 0

if K < A:
    score = K

else:
    score = A
    K = K - A
    if K > 0 and K < B:
        score = score
    elif K > 0 and K >= B:
        score = score
        K = K - B
        
        if K > 0:
            score = score - K

print(score)

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













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

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




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

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

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

Solution (Python):

a,b,c,k = input().split(' ')
out = 0
a = int(a)
b = a + int(b)
c = b + int(c)
k = int(k)

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

print(out)

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

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

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

count, score = 0, 0

c = min(A, (K - count))
score += 1 * c
count += c
if count < K:
    c = min(B, (K - count))
    score += 0 * c
    count += c

    if count < K:
        c = min(C, (K - count))
        score += -1 * c
        count += c

print(score)

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

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

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

Solution (Python):
def main():
	a,b,c,k = [int(x) for x in input().split(" ")]

	m = min(a, k)
	s = m
	if m <= k:
		k -= m
		m = min(b, k)
		if m <= k:
			k -= m
			m = min(c, k)
			s -= m

	print(s)

main()

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

    if a>k:
        print(k)
    elif a + b >= k:
        print(a)
    elif a+b<k:
        d=k-a-b
        print(a-d)
resolve()

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

if K <= A:
    ans = K
elif A+B >= K:
    ans = A
else:
    ans = 2*A+B-K

print(ans)

Solution (Python):
str=input().split()
A=int(str[0])
B=int(str[1])
C=int(str[2])
count=int(str[3])
if(count<=A):
    print(count)
elif(count<=(A+B)):
    print(A)
else:
    print(A-(count-A-B))