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 sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *
































def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007




def main():
	A,B,C,K = inputMap()

	ans = 0
	for i in range(K):
		if A > 0:
			if A >= K:
				print(K)
				sys.exit()
			else:
				K -= A
				ans += A
				A = 0
				
		elif B > 0:
			if B >= K:
				print(ans)
				sys.exit()
			else:
				K -= B
				B = 0
		else:
			print(ans-K)
			sys.exit()

	print(ans)






def sunuke(N, K, mod=10**9+7):
	if N < K or K-1 < 0:
		return 0
	else:
		return combination(N-1, K-1, mod)




def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv



def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)



if __name__ == "__main__":
	main()

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

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

Solution (Python):
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())
ans=0

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


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


print(ans-k)

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

if k < a:
    ans = a_num * 1
elif k < (a+b):
    ans = a_num * 1 + b_num * 0
else:
    ans = a_num * 1 - c_num * 1
print(ans)

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

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

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

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

Solution (Python):

a, b, c, K = map(int, input().split())
sum = 0

if K >= a:
  K = K - a
  sum += a*1
elif K != 0:
  sum += int(K)*1
  K = 0
  
if K >b:
  sum +=b*0
  K = K - b
elif K != 0:
  sum += int(K)*0
  K = 0
  
if K > c:
  sum +=c*-1
  K = K -c
elif K!=0:
  sum += int(K)*-1
  K = 0
print(sum)

Solution (Python):
Z = list(map(int, input().split()))
K=Z[-1]
A=Z[0]
B=Z[1]
C=Z[2]
k=K
ans=0
if k>A+B:
  ans=A*2-K+B
elif k>=A:
  ans=A
else:
  ans=k
  
print(ans)

Solution (Python):



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

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

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

if K < A:
    max = K
else:
    if K < A+B:
        max = A
    else:
        if K < A+B+C:
            max = A + B*0 + (K-A-B)*-1
        else:
            max = A + B*0 + C*(-1)

print(max)

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

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 + B - K)

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

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

Solution (Python):
inputs = [int(x) for x in input().split()]
sumAns = 0

if inputs[0] > inputs[3]:
	sumAns = sumAns + inputs[3]
	inputs[3] = 0
else:
	sumAns = sumAns + inputs[0]
	inputs[3] = inputs[3] - inputs[0]

if inputs[1] > inputs[3]:
	inputs[3] = 0
else:
	inputs[3] = inputs[3] - inputs[1]

if inputs[2] > inputs[3]:
	sumAns = sumAns - inputs[3]
	inputs[3] = 0
else:
	sumAns = sumAns - inputs[3]
	inputs[3] = inputs[3] - inputs[2]

print(sumAns)

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

print(x)

Solution (Python):
def F(A,B,C):
  return A-C;
[A,B,C,K]=input().split();
A=int(A);
B=int(B);
C=int(C);
K=int(K);
a=min(A,K);
b=min(K-a,B);
c=K-a-b

print(F(a,b,c));

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+b:
    print(min(k,a))
else:
    print(a-(k-(a+b)))

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

max_sum = 0

if a > k:
    max_sum = k
elif a+b >= k:
    max_sum = a
else:
    max_sum = a*2+b-k

print(max_sum)

Solution (Python):
import sys

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

if a < l:
    sm += a
    l -= a
else:
    sm += l
    print(sm)
    sys.exit()

if b < l:
    l -= b
else:
    print(sm)
    sys.exit()

sm -=  l
print(sm)

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

else:
    if chk2 > 0:
        ans = a -1*chk2
    else:
        ans = a

print(ans)

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

if a>k:
  print(k)
elif b>k-a:
  print(a)
elif c>k-a-b:
  print(a-(k-a-b))
else:
  print(a-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())

backup = K
AB = A + B
flag = False
ans = 0
if K <= A:
    print(K)
else:
    if A < K:
        K = K - A
        ans = ans + (A * 1)
    if B <= K:
        K = K - B
        flag = True
    if K > 0 and flag:
        ans = ans + (-1) * (backup - AB)
    print(ans)

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

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

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

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

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


sum = 0
flag = -1

if a>=k:
  flag = 1
  sum = k
else:
  sum = a
  k = k-a

if flag==-1:
  if b>=k:
    flag = 1
  else:k = k-b

if flag == -1:
  if c>=k:sum = sum -k
  else: sum = sum -c
    
print(sum)

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

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  and k <= a+b:
    print(a)
elif a + b < k and k <= a  + b + c:
    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)
else:
  print(a-(k-a-b))

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

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

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

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

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

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

nokori = K - A - B

wa = 0
while True:
    if A >= K:
        wa += K
        break
    else:
        wa += A
    if C >= nokori:
        wa -= nokori
        break
    else:
        wa -= C
        break

print(wa)

Solution (Python):
abck = list(map(int, input().split()))
a = abck[0]
b = abck[1]
c = abck[2]
k = abck[3]
if k <= a:
  print(k)
elif k <= a + b:
  print(a)
else:
  print(a - (k - (a + b)))

Solution (Python):
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


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

Solution (Python):
l = list(map(int,input().split()))
a = l[0]
b = l[1]
c = l[2]
k = l[3]
if (k-a-b) >0:
  print(a-1*(k-a-b))
else:
  print(min(a,k))

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

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

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

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

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

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

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

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

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

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

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

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

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

A,B,C,K=(int(x) for x in input().rstrip('\n').split())
res = A
if A<K:
  K-=A
  if B<K:
    K -= B
    res -= K
else:
  res =K
print(res)

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

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

Solution (Python):
import sys

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

cnt = k - a
if cnt >= 0:
  ans += a
  cnt = cnt - b
else:
  ans += k
  print(ans)
  sys.exit()
if cnt >= 0:
  cnt = cnt - c
  pass
else:
  print(ans)
  sys.exit()
if cnt >= 0:
  ans -= c
  print(ans)
else:
  ans -= k - (a + b)
  print(ans)

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

mx  = 0
rk = K

if rk > 0:
  mx += 1*min(A,rk)
  rk = rk - A
  
if rk >0:
  rk = rk - B
  
if rk>0:
  mx += -1*min(C,rk)
  
print(mx)

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

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

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

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

Solution (Python):

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


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

print(result)

Solution (Python):
A, B, C, K = [int(i) for i in input().split(" ")]
N = [A, B, C]
for i in range(len(N)):
    K -= N[i]
    if K == 0:
        if i == 0:
            N[i+1] = 0
            N[i+2] = 0
        elif i == 1:
            N[i+1] = 0
        break
    elif K < 0:
        if i == 0:
            N[i+1] = 0
            N[i+2] = 0
        elif i == 1:
            N[i+1] = 0
        N[i] += K
        break
print(1 * N[0] -1 * N[2])

Solution (Python):
t=list(map(int,input().split(" ")))
a=t[0]
b=t[1]
c=t[2]
k=t[3]
flag=0
sum = 0
if(a>k):
    sum=sum+k
    flag=1
    k=0
else:
    k=k-a
    sum=sum+a
    flag=1
    
if(flag==1):
    if(k!=0):
        if(b>k):
            k=0
            flag=2
        else:
            k=k-b
            flag=2
if(flag==2):
    if(k!=0):
        if(c>k):
            sum=sum-k
        else:
            k=k-c
            sum=sum-c
print(sum)

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

Solution (Python):
def main():
    import sys
    A,B,C,K=list(map(int,sys.stdin.readline().split()))
    o=0
    if K>=A:
        K-=A
        o+=A
    else:
        print(K)
        return

    if K>=B:
        K-=B
    else:
        print(o)
        return
    
    print(o-K)

main()

Solution (Python):
it = input()
it = it.split()

a = int(it[0])
b = int(it[1])
c = int(it[2])
k = int(it[3])

j = k
ot = 0

if a:
    if a < k:
        ot += a
        j -= a
    elif a >= k:
        ot += k 
        j = 0
    

if b and j != 0:
    if b >= j:
        j = 0
    elif b < j:
        j -= b

if c and j != 0:
    if c >= j:
        ot -= j
        j = 0
    elif j > c:
        ot -= c
        j -= c

print(ot)

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

an = a if a <= k else k
ans = an
k = k - an
if k > 0:
  bn = b if b <= k else k
  k = k - bn
if k > 0:
  cn = k
  ans = ans - cn
print(ans)

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

su = 0
card = 0

if k >= a:
    su = a
    card = k-a
    if card > b:
        card = card - b
        su = su - card
elif k < a:
    su = k

print(str(su))

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

suma = 0


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

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


if selectMax -plus <= 0:
    print(selectMax)

elif selectMax -plus-zero <= 0:
    print(plus)
    
else:
    print(plus-(selectMax -plus-zero))

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

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

Solution (Python):
inp = input().split(" ")

A = int(inp[0]); B = int(inp[1]); C = int(inp[2]); K = int(inp[3]);
point = 0

if (A > 0) & (K > 0):
  itr = min([A,K])
  point += (itr * 1)
  K -= itr

if (B > 0) & (K >0):
  itr = min([B,K])
  point += (itr * 0)
  K -= itr
  
if (C > 0) & (K >0):
  itr = min([C,K])
  point += (itr * -1)
  K -= itr

print(point)

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

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

Solution (Python):


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

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

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

if K <= A:
    print(K)
else:
    cards  = [A, B, C]
    points = [1, 0, -1]
    val = 0
    rem = K
    for i in range(3):
        rem -= cards[i]
        if rem <= 0:
            val += (cards[i] + rem) * points[i]
            break
        else:
            val += cards[i] * points[i]
    print(val)

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):
list=input()
list=list.split( )
count=0
a,b,c,k=int(list[0]),int(list[1]),int(list[2]),int(list[3])
if a>=k:
    count+=k
else:
    count+=a
    k=k-a
    if b>=k:
        yjsnpi=114514
    else:
        k=k-b
        count-=k
print(count)

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())
s=0
if k-a>=0:
    s=a
    k=k-a
    if k-b>=0:
        k=k-b
        if k-c==0:
            s=s-c
        elif k-c<0:
            s=s-k
    elif k-b<0:
        pass
elif k-a<0:
    s=k
print(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())
cnt = 0

if a>= k:
    print(k)
    exit()
else:
    k = k-a
    cnt +=a
if k >=b:
    k = k-b
if c >=k:
    cnt -=k
else:
    cnt -=c
print(cnt)

Solution (Python):
A, B, C, K = [int(_) for _ in input().split()]
print(min(K, A) - max(0, (K - A - B)))

Solution (Python):



s = input().rstrip().split(' ')
a = int(s[0])
b = int(s[1])
c = int(s[2])
k = int(s[3])
total = 0

    
    
if a > 0 and k > 0 :
    
    if k > a :
        total = a
    elif k < a :
        total = k
    elif k == a :
        total = a
    k = k - a
        

        
if b > 0 and k > 0 :
    k = k - b


if c > 0  and k > 0 :
    
    if k > c :
        total = total - c
    elif k < c :
        total = total - k
    elif k == c :
        total = total - c


print(total)

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=(int(x) for x in input().split())
num=0
if A >= K:
  num=K
elif K-A <= B:
  num=K-B
else:
  num=A-(K-(A+B))

print(num)

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

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

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

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



A,B,C,K = LI()

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

kt=k
sum=0

for i,card in enumerate([a,b,c]):
    if kt == 0:
        break
    if (kt - card) >= 0:
        if i == 0:
            sum+=card*1
        elif i == 1:
            sum+=card*0
        else:
            sum+=card*-1
        kt -= card            
    else:
        if i == 0:
            sum+=kt*1
        elif i == 1:
            sum+=kt*0
        else:
            sum+=kt*-1
        kt = 0

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

Solution (Python):
s = input().split()

if s[3] == "0" :
    print(0)
    
elif ( int(s[0]) >= int(s[3]) ) :
    print(s[3])

elif ((int(s[3]) - int(s[0]) <= int(s[1]))):
    print(s[0])
elif int(s[3]) - int(s[0]) > int(s[1]) :
    print(int(s[0])- (int(s[3])-int(s[0])-int(s[1])))

Solution (Python):
input_list=list(input().split(" "))


a = int(input_list[3]) - int(input_list[0])
b = int(input_list[3]) - int(input_list[1])- int(input_list[0])
c = int(input_list[3]) - int(input_list[2])- int(input_list[1])- int(input_list[0])
if a < 0:
    calc = int(input_list[3])
elif b <= 0 and a >=0:
  calc = int(input_list[0])
else:
  calc = int(input_list[0]) - b
print(calc)

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

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

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

Solution (Python):
lis=list(map(int,input().split()))
A=lis[0]
B=lis[1]
C=lis[2]
K=lis[3]
ans=0
if(A>=K):
    ans=K
else:
    if(A+B>=K):
        ans=A
    else:
        c=K-A-B
        ans=A-c
print(ans)

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

cnt_a = 0
cnt_b = 0
cnt_c = 0

if (a > 0) & (a > k):
    cnt_a = k
    k = 0
else:
    cnt_a = a
    k = k - cnt_a

if (b > 0) & (b > k):
    cnt_b = k
    k = 0
else:
    cnt_b = b
    k = k - cnt_b

if c > 0:
    cnt_c = k

result = cnt_a * 1 + cnt_b * 0 + cnt_c * (-1)
print(result)

Solution (Python):
import sys

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

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

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

score=0

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

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

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

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



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

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

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

Solution (Python):


from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

def mulinputs():
    return map(int,input().split())

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    A, B, C, K = mulinputs()
    
    if K < A:
        print(K)
        exit()
    if K <= A+B:
        print(A)
        exit()
    K -= (A+B)
    print(A-K)