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):
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor



dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
a, b, c, k = map(int, input().split())
ans = 0
if a <= k:
    ans += a*1
if a > k:
    ans += k*1
    print(ans)
    exit()
if b <= (k-a):
    ans += b*0
if b > (k-a):
    ans += 0*(k-a)
    print(ans)
    exit()
if c <= (k-a-b):
    ans += c*(-1)
    print(ans)

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

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

count = 0

count = min(A, K)
max = count
K -= count

count = min(B, K)
K -= count

max += K * (-1)
print (max)

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

Solution (Python):
import sys

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

ans = 0
tmp = k
if a < k:
    ans += a
    tmp -= a
else:
    ans += k
    print(ans)
    sys.exit()

if b < tmp:
    tmp -= b
else:
    print(ans)
    sys.exit()

if c < tmp:
    ans -= c
else:
    ans -= tmp
print(ans)

Solution (Python):



import sys

def main():
    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))
    else:
        print(A-C)

if __name__ == '__main__':
    main()

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

if k < a:
	print(k)

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

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

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

B_number = 0
C_number = 0

A_number = min(a,k)
k1 = k - a
if k1 > 0:
    B_number = min(b,k1)
    k2 = k1 - b
    if k2 > 0:
        C_number = min(c,k2)

print(A_number-C_number)

Solution (Python):
inp = input()
inp = inp.split()
a = int(inp[0])
b = int(inp[1])
c = int(inp[2])
k = int(inp[3])
if k <= a:
    print(k)
else:
    sum = a
    k = k - a
    if k <= b:
        print(sum)
    else:
        k = k - b
        sum = sum - k
        print(sum)

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

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

Solution (Python):
def getls():
    return [ipt for ipt in input().split()]
def getlsi():
    return [int(ipt) for ipt in input().split()]

[a,b,c,k] = getlsi()
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) <= 0:
  print(K)
else:
  if (K - A - B) <= 0:
    print(A)
  else:
    print(2*A - K + B)

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

t=0
for c,v in [[A,1],[B,0],[C,-1]]:
  if k>c:
    t+=c*v
    k-=c
  else:
    t+=k*v
    break

print(t)

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

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

xa = min(a, k)
k -= xa
xb = min(b, k)
k -= xb
xc = k
res = xa - xc
print(res)

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

Solution (Python):


plus, zero, minus, getNum = map(int, input().split())

ans = 0

if getNum <= plus:
    ans += getNum
    print(ans)
    quit()
else:
    ans += plus
    getNum -= plus

if getNum <= zero:
    print(ans)
    quit()
else:
    getNum -= zero

if getNum <= minus:
    ans -= getNum
else:
    ans -= minus
print(ans)

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())
print(min(a,k)-max(k-a-b,0))

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

score = 0

if K >= A:
  score += A
  K -= A
else:
  score += K
  print(score)
  exit()

if K >= B:
  K -= B
else:
  print(score)
  exit()

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

Solution (Python):
def a():
    s = input()
    t = input()
    if s == t[0:-1]:
        print('Yes')
    else:
        print('No')



def b():
    a, b, c, k = map(int, input().split())
    xa = min(k, a)
    k1 = k - xa
    xb = min(k1, b)
    k2 = k1 - xb
    print(xa - k2)






b()

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

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

Solution (Python):
t=list(map(int,input().split()))
l=[1,0,-1]
k=t[3]
s=0
i=0
while(k!=0):
  if(k>t[i]):
    s+=l[i]*t[i]
    k-=t[i]
  else:
    s+=l[i]*k
    k=0
  i+=1
print(s)

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

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

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

ans = 0
ans += min(a, k)

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

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

count=0
if a<k:
    if b<k-a:
        count=a-(k-a-b)
    else:
        count=a
else:
    if a==k:
        count=a
    else:
        count=k

print(count)

Solution (Python):
import sys,math,random

input = sys.stdin.readline
 

def inp():
    return(int(input()))
def inara():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split()))



a,b,c,k=invr()

ans=0
if k:
	ans+=min(a,k)
	k-=min(a,k)
if k:
	ans+=0*(min(b,k))
	k-=min(k,b)
if k:
	ans-=min(c,k)
	k-=c

print(ans)

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

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

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

if K<=A:
    print(K)

elif A<K and K<B:
    print(A)

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

Solution (Python):
[A,B,C,K] = list(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())

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

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

if k >= c:
    ans -= c
    print(ans)
else:
    ans -= k
    print(ans)

Solution (Python):

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



if k<=a+b:
    ans=min(a,k)
else:
    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(2*a + b - k)

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

ans = 0
for i in range(len(nList)):
  if nList[i] <= K:
    ans += nList[i]*pList[i]
    K -= nList[i]
  else:
    ans += K*pList[i]
    break
    
print(ans)

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

ka = min(a,k)
k -= ka
kb = min(b,k)
k -= kb
kc = min(c,k)
k -= kc

ans = ka - kc
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 = [int(i) for i in input().split()]


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

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):
arr = [int(x) for x in input().split(' ')]
a, b, c, k = arr
ans = 0
mn = min(a, k)
ans += mn
k-=mn
mn = min(b, k)
k-=mn
mn = min(c, k)
ans -= mn

print(ans)

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

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

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

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):
a, b, c, k = [int(x) for x in input().split()]
a_x = min(a, k)
k -= a_x
b_x = min(b, k)
k -= b_x
c_x = min(c, k)
ans = a_x - c_x
print(ans)

Solution (Python):
from sys import stdin

a, b, c, k = [int(x) for x in stdin.readline().rstrip().split()]

x = 0

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

print(x)

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

xa = min(A,K)
K1 = K -xa
if K1 == 0:
  print(K)
else:
	xb = min(B,K1)
	K2 = K1 -xb
	if K2 == 0:
  		print(A)
	else:
   		print(A-K2)

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

Solution (Python):
A,B,C,K = list(map(int,input().split()))
"""
for n in range(1):
    n = list(map(int,input().split()))
    L.append(n)
"""

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

ans = 0

ans += min(a, k)
k -= min(a,k)

ans += min(b, k) * 0
k -= min(b,k)

ans += min(c,k) * -1
k -= min(c,k)

print(ans)

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(2*a + b - k)

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

ans = 0
n = min(A, K)
ans += n
K -= n

n = min(B, K)
K -= n

n = min(C, K)
ans -= n

print(ans)

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

res = 0

res += min(K, A)
K -= A

if K > 0:
    K -= B
if K > 0:
    res -= min(K, C)

print(res)

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-B-A))
else:
  print(A-C)

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

ans = 0

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

print(ans)

Solution (Python):
l = [int(x) for x in input().split()]
A, B, C, K = l[0], l[1], l[2], l[3]
ans = 0
while K > 0:
    if A > 0:
        if A > K:
            ans += K
            K = 0
        else:
            ans += A
            K -= A
            A = 0
    elif B > 0:
        if B > K:
            K = 0
        else:
            K -= B
            B = 0
    else:
        ans -= K
        K = 0
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9 + 7
input = sys.stdin.readline

def inp():
    return int(input())
def inpl():
    return list(map(int,input().split()))
def inpm():
    return map(int,input().split())

a,b,c,k = inpm()

print(min(min(k,a),2*a+b-k))

Solution (Python):
l = list(map(int, input().split()))
p = 0
xa = min(l[0], l[3])
l[3] -= xa
xb = min(l[1], l[3])
l[3] -= xb
xc = l[3]
p = xa - xc
print(p)

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




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

    result = min(A, K)
    K -= (A + B)
    result -= max(0, K)

    print(result)


if __name__ == "__main__":
    main()

Solution (Python):

class abc_167:
    
    def __init__(self):
        pass
    
    def A(self):
        S=input()
        T=input()
        if S==T[:len(S)]:print("Yes")
        else:print("No")
    
    def B(self):
        ABCK=list(map(int,input().split()))
        sum_num=0
        
        if ABCK[3]<=ABCK[0]:sum_num=ABCK[3]
        elif ABCK[3]<=ABCK[0]+ABCK[1]:sum_num=ABCK[0]
        else:sum_num=ABCK[0]-(ABCK[3]-ABCK[0]-ABCK[1])
        
        print(sum_num)
            
    def C(self):
        pass
            
    def D(self):
        
        NK=list(map(int,input().split()))
        A=list(map(int,input().split()))
        stack=[]
        A_memo=[0]*(NK[0]+1)
        flag=False
        
        
        count=0
        stack.append(A[count])
        count+=1
        A_memo[1]=count
        
        max_data = NK[0]+1 if NK[0]<=NK[1] else NK[1]
        
        while(count<max_data):
            data=stack.pop()
            stack.append(A[data-1])
            count+=1
            
            if A_memo[A[data-1]]==0:
                A_memo[A[data-1]]=count
            else:
                loop_val=A[data-1]
                flag=True
                break
            
        
        
        
        
        
        ans_stack=[]
        ans_stack.append(A[0])
            
        if flag==True:
            ans=((NK[1]-A[loop_val])%(count-A[loop_val]))+(A[loop_val]-1)
            for i in range(ans):
                ans_data=ans_stack.pop()
                ans_stack.append(A[ans_data-1])
            end=ans_stack.pop()
            print(end)
        else:
            ans=count
            for i in range(ans):
                ans_data=ans_stack.pop()
                ans_stack.append(A[ans_data-1])
            end=ans_stack.pop()
            print(end)
            
            
           
            
    
    def E(self):
        pass
    
    def F(self):
        pass
    
    
if __name__=='__main__':
    obj=abc_167()
    obj.B()

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

count = 0

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

print(count)

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

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

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

xa = min(a, k)
k -= xa
xb = min(b, k)
k -= xb
xc = k
ans = xa - xc

print(ans)

Solution (Python):
z=input().split(' ')

a=int(z[0])
b=int(z[1])
c=int(z[2])
d=int(z[3])

count=0
if d-a>0:
    count+=a
else:
    count=d

x=d-a



if x-b>0:
    y=x-b
    
    if y-c>0:
        count-=c
    else:
        count-=y
else:
    pass

print(count)

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

Solution (Python):

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

    ans = 0
    used = min(k,a)
    ans += used * 1
    k -= used
    used = min(k, b)
    k -= used
    ans += k*(-1)
    print(ans)


if __name__ == "__main__":
    resolve()

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

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

xa = min(A, K)
K = K - xa
xb = min(B, K)
K = K - xb
xc = min(C, K)
total = xa*1 + xb*0 + xc*-1
print(total)

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

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



def main():
    number_of_A,number_of_B,number_of_C,nubmer_of_take_cards = map(int,input().split())
  
    result = 0
    if number_of_A >= nubmer_of_take_cards:
        result = nubmer_of_take_cards
    else:
        result += number_of_A
        nubmer_of_take_cards = nubmer_of_take_cards - number_of_A
        
        if number_of_B < nubmer_of_take_cards:
            nubmer_of_take_cards = nubmer_of_take_cards - number_of_B
            result = result - nubmer_of_take_cards
    
    print(result)
            
if __name__ == "__main__":
    main()

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

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

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


main()

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

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

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

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

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

xa = min(a, k)
k -= xa
xb = min(b, k)
k -= xb
xc = k
ans = xa - xc
print(ans)

Solution (Python):
def main():
    inp = input()
    data = inp.split(" ")
    data = [int(i) for i in data]
    a = data[0]
    b = data[1]
    c = data[2]
    k = data[3]

    if a > k:
        print(k)
        return

    r = k - a
    score = a

    if b > r:
        print(score)
        return

    r = r - b

    if c > r:
        score -= r
    else:
        score -= c

    print(score)


if __name__ == "__main__":
    main()

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


X_A = min(A, K)

K_A = K - X_A


X_B = min(B, K_A)
K_B = K_A - X_B


X_C = min(C, K_B)

print(X_A - X_C)

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

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

print(ans)

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

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):
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))

def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)

MOD = 998244353
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline

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

Solution (Python):
a, b, c, k = map(int, input().split())
pa = min([a, k])
k -= pa
pb = min([b, k])
k -= pb
pc = min([c, k])
print(pa - pc)

Solution (Python):
import itertools
import math
import fractions
import functools

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

Solution (Python):
import itertools
import math
import fractions
import functools

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

Solution (Python):


import sys

input = iter(sys.stdin.readlines()).next

def main():
   A, B, C, K = map(int, input().split())
   res = 0
   if A >= K:
      res = 1*K
   else:
      res += A
      K -= A
      if B >= K:
         res += 0*K
      else:
         res += 0*B
         K -= B
         res += -1*K

   print(res)

   return 0

if __name__ == '__main__':
   sys.setrecursionlimit(10000)
   main()

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())
ans = min(a, k)
k -= min(a, k)

ans += min(b, k)*0
k -= min(b, k)

ans += min(c, k)*(-1)
k -= min(c, k)

print(ans)

Solution (Python):
a, b, c, k = map(int, input().split())
if k <= a+b:
    print(min(k, a))
else:
    print(2*a-k+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 = [int(x) for x in input().split()]

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

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

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

Solution (Python):
a,b,c,k = map(int,input().split())
arr = [a,b,c]
ans = 0
for i,item in enumerate(arr):
  val = min(k,item)
  k-=val
  ans+=val*(1-i)
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):
a, b, c, k = map(int, input().split())

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

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

A, B, C, K = map(int, input().rstrip().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)
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):
from sys import stdin, stdout
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	a, b, c, k = rli()
	ans = 0
	for add, cnt in [(1, a), (0, b), (-1, c)]:
		score = min(k, cnt)
		ans += add*score
		k -= score
		if k == 0:
			break
	print(ans)
	stdout.close()

if __name__ == "__main__":
	main()

Solution (Python):

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


result_score = 0
for score, i in [(1, A), (0, B), (-1, C)]:
        if K <= 0:
               continue

        if K >= i:
            result_score += score * i
            K -= i
        else:
            result_score += score * K
            K = 0

print(result_score)

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:
  print(a-(k-a-b))

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

Solution (Python):
a,b,c,k=(int(x) for x in input().split())
if a+b >= k:
  print(min(k,a))
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 = [int(i) for i in input().split()]

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

Solution (Python):
a,b,c,k=map(int,input().split())
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())

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

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

Solution (Python):
def B():
    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)*-1)
B()

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

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

ans = 0

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

print(ans)

Solution (Python):
import sys
 
A, B, C, K = map(int, sys.stdin.readline().split())
res = 0
res += min(A, K)
K -= A
if K > 0:
  K -= B
if K > 0:
  res -= min(C, K)
print(res)

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

xa = min(a,k)
k -= xa
xb = min(b,k)
k -= xb
xc = k
ans = xa - xc
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):

def main():
    import sys
    imput = sys.stdin.readline
    
    
    A, B, C, K = map(int, input().split())

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

if __name__ == "__main__":
    main()

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

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

print(score)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    a,b,_,k=map(int,input().split())
    if k<=a:
        print(k)
    elif k>a and k<=a+b:
        print(a)
    else:
        print(a-(k-a-b))
if __name__=='__main__':
    main()

Solution (Python):


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

Solution (Python):

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

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

result=0
t=min(A,K)
result+=t
K-=t
t=min(B,K)
K-=t
t=min(C,K)
result-=t
print(result)

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

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

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

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

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

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

sum = 0

X_a = min(A,K)
sum += X_a
K -= X_a

X_b = min(B,K)
K -= X_b

X_c = min(C,K)
sum -= X_c

print(sum)

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

ans = 0
x = min(k , a)
ans += x
k2 = k - x
x2 = min(k2, b)
k3 = k2 - x2
x3 = min(k3, c)
ans -= k3

print(ans)

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

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())
cards = [b, a, c]
print(min(k, cards[1]) - max(k - cards[0] - cards[1], 0))

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

sum = 0

for i in range(3):
    if a[3] <= 0:
        break
    if a[i] >= a[3]:
        sum += (1 - i) * a[3]
    else:
        sum += (1 - i) * a[i]
    a[3] -= a[i]
        
print(sum)

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

sum = 0

if a[0] >= a[3]:
    sum += 1 * a[3]
else:
    sum += 1 * a[0]

a[3] -= a[0]
a[3] -= a[1]

if a[3] > 0:
    if a[2] > a[3]:
        sum += -1 * a[3]
    else:
        sum += -1 * a[2]

print(sum)

Solution (Python):
a,b,c,k=map(int,input().split())
m=min(a,k)
ans=m
k-=m
m=min(b,k)
k-=m
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 - (k - a - b))

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

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

Solution (Python):

ABCK = input().split(' ')
A = int(ABCK[0]) ; B = int(ABCK[1]) ; C = int(ABCK[2])
K = int(ABCK[3])
if K <= A:
    print(K)
elif K <= (A + B):
    print(A)
else:
    print(A - (K - A - B))

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


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

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

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

Solution (Python):
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

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

print(a - c)

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