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):
def resolve():
    A, B, C, K= map(int, input().split())

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

if __name__ == "__main__":
    resolve()

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

count = 0

if k >= 0:
  if a >= k:
    count += k
    k -= a + b
  else:
    count += a
    k -= a + b

  if k >= 0:
    count -= k

print(count)

Solution (Python):

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





def solve():
    k = K
    ans = 0
    if A - k >= 0:
        print(k)
        return

    if A - k < 0:
        ans += A
        k -= A

    if B - k >= 0:
        print(ans)
        return

    if B - k < 0:
        k -= B

    ans -= k

    print(ans)

solve()

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

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

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

else:
    print(k)

Solution (Python):
S = input("")
l = S.split()
A = int(l[0])
B = int(l[1])
C = int(l[2])
K = int(l[3])
sum = 0
while(K > 0):
  if A > 0:
    K = K - A
    sum+=A
    A = 0
    if K < 0:
    	sum+=K
  elif B > 0:
    K = K - B
    B = 0
  elif C > 0:
    K = K - C
    sum-=C
    C = 0
    if K < 0:
    	sum-=K
print(sum)

Solution (Python):
"""
https://atcoder.jp/contests/abc167/tasks/abc167_b
"""
import numpy as np
from numba import jit


@jit(nopython=True, parallel=False)
def calc(A: int, B: int, C: int, K: int) -> int:
    if A >= K:
        return K
    if A + B >= K:
        return A
    return A - (K - (A + B))


def load_input_as_int_array() -> np.ndarray:
    return np.array([int(v) for v in input().split(" ")])


def main() -> None:
    A, B, C, K = load_input_as_int_array()
    result = calc(A, B, C, K)
    print(result)
    


main()

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

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

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

Solution (Python):


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

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

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

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 A < K <= A+B:
    print(A)
elif A+B < K <= A+B+C:
    x = A-(K-A-B)
    print(x)

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

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

k2 = k1 - b
if k2 > 0:
    pass
else:
    print(cnt)
    exit(0)

k3 = k2 - c
if k3 > 0:
    cnt -= k3
else:
    cnt -= k2

print(cnt)

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

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

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

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

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

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

count = 0

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

Solution (Python):
A, B, C, K = tuple(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)
elif a<k and k<=a+b:
	print(a)
elif a+b<k and k<=a+b+c:
	print(a-(k-a-b))
elif a+b+c<=k:
	print(a-c)

Solution (Python):
a,b,c,k=map(int,input().split())
count=0
mai=0
if k<=a:
    print(k)
    exit()
if a+b>=k:
    print(a)
    exit()
if a+b<k:
    cc=k-(a+b)
    print(a-cc)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
MOD = 10**9 + 7

a,b,c,k = map(int,input().split())
ans = 0
if k>0:
    ans += min(k,a)
    k -= min(k,a)
if k>0:
    k-= min(k,b)
if k>0:
    d = min(c,k)
    ans += -1*d
print(ans)

Solution (Python):
import sys

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






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

if K > C:
  sum += (-C)
else:
  sum += (-K)
  print(sum)
  sys.exit()

print(sum)

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

    sum = 0
    rem_num = k

    if(a >= k):
        sum = sum + k
        print(sum)
        return
    
    if(k > a and k <= a + b):
        sum = sum + a
        print(sum)
        return
    
    if(k > a + b ):
        sum = sum + a - 1*(k - a - b)
        print(sum)
        return


if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
S = input().split()
a = int(S[0])
b = int(S[1])
c = int(S[2])
k = int(S[3])

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

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

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

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

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

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

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

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

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

if (K < A):
    print(K)
    exit()
elif(K >= A):
    if (K < A + B):
        print(A)
        exit()
    elif (K >= A + B):
        print(A + (-1) * (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(-K+B+2*A)

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

  ans = 0

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

if __name__ == "__main__":
  main()

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

tmp = k
k = k - a
if k > 0:
    ans = a
else:
    ans = tmp

if k > 0:
    k = k - b

if k > 0:
    ans = ans - k

print(ans)

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

if k>=a:
 res=res+a
 if k-1>=b:
   res=res+b-b
   if k-(a+b)>=c:
    res=res+c-2*c
   else:
    res=res+(k-a-b)-2*(k-a-b)
 else:
   res=res+(k-a)-(k-a)
else:
 res=res+k
print(res)

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

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

Solution (Python):
import math

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

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

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

print(_sum - k)

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

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

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

Solution (Python):
a,b,c,k=map(int,input().split())
ans=(k if a>=k else a)
k-=min(k,a)
k-=min(k,b)
ans-=(k if c>=k else c)
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(-K+B+2*A)

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())
a = 0
if K == A:
    print(A)
elif K < A:
    print(K)
else:
    a += A
    K -= A
    if K > B:
        K -= B
        if K > C or K == C:
            a -= C
            print(a)
        else:
            a -= K
            print(a)
    else:
        print(a)

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

a = min(A, K)
b = max(K - a, 0)
b = min(b, B)
c = max(K - a - b, 0)

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

Solution (Python):
one, zero, neg_one, k = map(int, input().split())
print(min(one, k) - max(0, k-one-zero))

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


if(K>0):
    if(K>B):
        K-=B
    else:
        K=0


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

Solution (Python):
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd 
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) 

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

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

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

k

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())
d = k - a
e = d - b
f = k -(a + b)
if d <= 0:
    print(1 * k)
else:
    print(a + f * (-1))

Solution (Python):
data = input().rstrip().split(" ")
one = int(data[0])
zero = int(data[1])
minus = int(data[2])
num = int(data[3])
answer = 0
if one >= num:
    answer = num
elif one + zero >= num:
    answer = one
else:
    answer = one * 2 + zero - num
print(answer)

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

print(r)

Solution (Python):
a,b,c,k = list(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=list(map(int,input().split()))
ans=0
if(k<=a):
  ans=k
elif(a<k and k<=a+b):
  ans=a
elif(a+b<k and k<=a+b+c):
  ans=a-(k-a-b)

print(ans)

Solution (Python):

A,B,C,K=map(int,input().split())
cnt=0
if K-A > 0:
  cnt=A
  if K-A-B >0:
    cnt=cnt-(K-A-B)
    print(cnt)
  else:
    print(cnt)
else:
  print(K)

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

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

if __name__ == '__main__':
    solve()

Solution (Python):


import sys
import math


debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        
        
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

   
    (a, b, c, k) = [int(e) for e in lines[0].split(' ')]

    return (a, b, c, k)


def solve(a, b, c, k):
    
    result = None
    if a >= k:
        result = 1 * k
    elif a + b >= k:
        result = 1 * a
    elif a + b + c >= k:
        result = 1 * a + 0 * b - 1 * (k - (a + b))
    else:
        result = 1 * a + 0 * b - 1 * c

    return result

    


def main():
    
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

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

Solution (Python):
import math
from sys import stdin,stdout

from heapq import heapify,heappop,heappush

from collections import Counter


    
n1,n0,nm,k=list(map(int,stdin.readline().split()))

ans=0
if n1+n0>=k:
    ans= min(n1,k) 
    print(ans)
    
else:
    ans+=n1
    ans-= k-n1-n0
    print(ans)

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

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())
ans = 0
bool =True
if k > a:
    ans = ans +a
    k = k-a 
    if k > b:
        k = k-b
        if k > 0:
            ans -= k
    
        
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(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:
    n = k - (a + b)
    ans = a + -1 * n
    print(ans)

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

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

if A >= K:
  ans = K
elif A + B >= K > A:
  ans = A
elif K > A + B:
  ans = A - (K - A - B)
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(-k+a+a+b)

Solution (Python):

A, B, C, K = map(int, input().split())
rt = A if A < K else K
K = K - A - B
if K > 0:
  rt -= K
print(rt)

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

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

Solution (Python):
one,zero,minus,k = map(int,input().split())
result = 0
if one > k:
    print(k)
else:
    result += one
    k -= one
    if zero > k:
        print(result)
    else:
        k -= zero
        if k > 0:
            result -= k
        print(result)

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

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

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

count = 0
second = K-A-B

if A >= K:
  count = K
else:
  count = A

if second > 0:
    count -= second

print(count)

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

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

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

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 = map(int, input().split())

total = 0
if(a < k):
    total = a
    k = k - a
    if(b < k):
        k = k - b
        if(c < k):
            total += c * -1
        else:
            total += k * -1
    else:
        pass
else:
    total = k

print(total)

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 = map(int,input().split())
cnt = 0
if K-A>0:
  cnt += A
  if K-A-B>0:
    if K-A-B-C>0:
      cnt -= C
    else:
      cnt -= K-A-B
else:
  cnt += K
print(cnt)

Solution (Python):
def main(): 
    N = [int(_) for _ in input().split()]
    A = N[0]
    B = N[1]
    C = N[2]
    K = N[3]

    result = 0
    if A >= K:
        print(K)
        return
    if A < K:
        K = K - A
        result = A
    if B >= K:
        print(result)
        return
    if B < K:
        K = K - B
    if C >= K:
        result = result - K
        print(result)
        return
    if C < K:
        result = result - C
        print(result)
        return

if __name__ == "__main__":
    main()

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

aa, bb, cc = 0, 0, 0

if a >= k:
    aa = k
else:
    aa = a
    k = k - a
    
    if b >= k:
        bb = k
    else:
        bb = b
        k = k -b

        if c >= k:
            cc = k
        else:
            cc = c
            k = k -c

print(aa -cc)

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 - k + b)

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

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

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

print(ans)

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

Solution (Python):
def t1():
    ll=list(map(int,input().split()))
    K=ll[-1]
    S=[1,0,-1]
    wa=0
    
    if K>ll[0]:
        wa+=ll[0]
        K-=ll[0]
        if K>ll[1]:
            K-=ll[1]
            wa-=K
        else:
            pass
    else:
        wa+=K
    

    print(wa)
        
            

      
if __name__ == '__main__':
    t1()

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

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

v = [1, 0, -1]
val = 0
for idx, i in enumerate([A, B, C]):
    val += v[idx] * i
    if K - i <= 0:
        print(val - v[idx] * (i - K))
        break
    else:
        K -= i

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

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

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

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

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

Solution (Python):
import itertools

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

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

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

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

Solution (Python):
A, B, C, K = list(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 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 (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()))
if a>=k:
    print(k)
elif a+b>=k and k>a:
    print(a)
else:
    k-=a+b
    print(a-k)

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

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

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

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

Solution (Python):
def next(a,b,c,k):
	
	s = 0
	if k >= a:
		s += a
		k -= a
	else:
		print(k)
		return
	if k >= b:
		k -= b
	else:
		print(a)
		return

	print(s-k)
	return


    




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


next(a,b,c,k)
'''arr = list(map(int,input().split()))
qarr = []
for i in range(b):
	qarr.append(int(input()))



Sol(a,b,arr,qarr)'''

Solution (Python):
def solve():
    A, B, C, K = map(int, input().split())
    if A >= K >= 1:
        print('{}'.format(K))
    elif A + B >= K > A:
        print('{}'.format(A))
    elif A + B + C >= K > A + B:
        result = 2 * A + B - K
        print('{}'.format(result))

        return


if __name__ == "__main__":
    solve()

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

Solution (Python):
def main():
    A,B,C,K = map(int, input().split())
    score=0
    count=K
    i=0
    while count>0:
        if i==0:
            if A<=K:
                score=score+A
            else:
                score=K
            
            count=count-A
            
        elif i ==1:
            count=count-B
            
        else:
            score=score-count
            count=count-C
            
        i=i+1
        
    print(score)
main()

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

if a < k :
  max = a
  k = k - (a + b)
  max += -1 * k
  
else :
  max = k

print(max)

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




print(sum_max)

Solution (Python):
a, b, c, k = [int(i) for i in input().strip().split()]
result = 0

result = min(a, k)
if k > (a + b):
    result -= min((k - a - b), c)
print(result)

Solution (Python):
import sys
input = sys.stdin.readline
def calc(A, B, C, K):
    sum = 0
    if K>A:
        sum += A
        K = K-A
    else:
        sum = K
        return sum
    if K>B:
        K = K - B
    else:
        return sum
    sum = sum - K
    return sum


def main():
    A, B, C, K = map(int, input().split())
    sum = calc(A,B,C,K)
    print(sum)

    

if __name__ == '__main__':
    main()

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

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

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

s += min( a , k)
k -=a
k -=b
s -= max(k , 0)
print(s)

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

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

s = K - A
m = 0
if s >= 0:
  m += A
else:
  print(K)
  exit()
if s - B >= 0:
  s -= B
else:
  print(m)
  exit()
if s - C >= 0:
  print(m + (-C))
else:
  print(m - s)

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

result = 0
if K <= A+B:
  result = min(A, K)
else:
  result = A + (-1)*(K-A-B)
  
print(result)

Solution (Python):
d=input()
d1=d.split()
a=int(d1[0])
b=int(d1[1])
c=int(d1[2])
x=int(d1[3])
if a+b>=x:
    if a>x:
        print(x)
    else:
        print(a)
else:
    print(a-(x-a-b))

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

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())
cnt = 0
if A > K:
    print(K)
    exit()
if A+B >= K:
    print(A)
else :
    print(A-max(0,(K-A-B)))

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

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

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

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

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

'''
'''
n = input()
a = [int(input()) for i in range(n)]
'''
'''
n,m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(m)]
'''
 



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

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))

def resolve():
    A,B,C,K=pin()
    ans=K
    if A<K:ans=A
    K-=(A+B)
    if K<0:print(ans);return
    print(ans-K)

resolve()

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

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()


def main():
    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))

if __name__ == "__main__":
    main()

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

Ka = max(0, min(K, A))
Kb = max(0, min(B, K-Ka))
Kc = max(0, min(C, K-Ka-Kb))

output = 1*Ka + 0*Kb + (-1)*Kc
print(output)

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 =input().split()
if int(K) <= int(A):
  print(K)
  
elif int(K) <= int(A) + int(B):
  print(A)
else:
  print(str(2*int(A) + int(B) - int(K)))

Solution (Python):
cards = [int(s) for s in input().split()]
if (cards[3] - cards[0])<= 0:
  print(cards[3])
else:
  d = cards[3] - (cards[0] + cards[1])
  if d > 0:
    print(cards[0]-d)
  else:
    print(cards[0])

Solution (Python):

url = "https://atcoder.jp/contests/abc163/tasks/abc163_b"

def main():
    t = list(map(int, input().split()))
    ans = 0

    li = []
    for i in range(3):
        tmp = 0
        if t[i] <= t[-1]:
            tmp = t[i]
            t[-1] -= t[i]
        elif t[i] > t[-1]:
            tmp = t[-1]
            t[-1] -= t[-1]
        li.append(tmp)
    print((li[0] * 1) + (li[1] * 0) + (li[2] * -1))









if __name__ == '__main__':
    main()

Solution (Python):
import sys

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

if a >= k:
  print(k)
  sys.exit()


score += a
num_card -= a
  

if num_card <= b:
  print(score)
  sys.exit()
else:
  num_card -= b


if num_card >= c:
  score = score - c
else:
  score = score - num_card 
  
print(score)

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

mycnt = 0
myans = 0

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

Solution (Python):
A = list(map(int , input().split()))
if A[0] < A[-1]:
    a = A[0]
    b = A[-1] - A[0]
    if A[1] < b:
        a -= (b - A[1])
elif A[-1] < A[0]:
    a = A[-1]
else:
    a = A[0]
print(a)

Solution (Python):


from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.buffer.readline
INF = float('inf')

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

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

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


def main():
    a, b, c, k = map(int, input().split())
    total = 0

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

if __name__ == '__main__':
    main()

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

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

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

if K <= A :
    ans = K
elif A < K <= A + B :
    ans = A
elif A + B < K <= A + B + C :
    ans = A - (K - A - B)

print(ans)

Solution (Python):
a,b,c,k=map(int,input().split())
h=k-a
h2=h-b
if a>k:
  print(k)
else:
  if h<=b:
    print(a)
  else:
    print(a+h2*(-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 = [int(i) for i in input().split()]
res = min(k, a) + min(max(k - a, 0), b) * 0 + min(max(k - a - b, 0), c) * (-1)
print(res)

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

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