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

Line = [int(s) for s in input().split()]

A,B,C,K = Line[0],Line[1],Line[2],Line[3]

ans = 0

if A >= K:
    ans += K
    print(ans)
    sys.exit()
else :
    ans += A
    K -= A

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

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

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

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

Solution (Python):
from collections import *

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

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
   
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

cnt=0

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

if k>0:
	k-=b
	if k>0:
		cnt-=k
		
print(cnt)

Solution (Python):

import sys

sysread = sys.stdin.readline
read = sys.stdin.read


sys.setrecursionlimit(10**7)




def run():
    A,B,C,K = map(int, sysread().split())
    ret = 0
    for i, x in zip((1,0,-1), (A, B, C)):
        tmp = min(x,K)
        K -= tmp
        ret += tmp * i
        if K == 0:break
    print(ret)
if __name__ == "__main__":
    run()

Solution (Python):

import sys


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    A, B, C, K = IL(int)
    if K <= A:
        print(K)
    elif K <= A + B:
        print(A)
    else:
        print(A - (K - A - B))


solve()

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


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

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

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

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

Solution (Python):
def resolve():
    '''
    code here
    '''
    A, B, C, K = [int(item) for item in input().split()]

    A_max = min(A, K)
    B_max = min(B, K - A_max)
    C_max = K - A_max - B_max

    print(A_max - C_max)

if __name__ == "__main__":
    resolve()

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

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

Solution (Python):
def run():
    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))


if __name__ == '__main__':
    run()

Solution (Python):
A, B, C, K = map(int, input().split())
one = min(A, K)
zero = max(0, min(B, K - one))
minus = max(0, K - one - zero)
ans = one - minus
print(ans)

Solution (Python):
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())


    
a,b,c,k=mi()
s=0
if(k>=a):
    s+=a
    k-=a
    if(k>=b):
        k-=b
    
    if(k>0):
        s-=k
    print(s)
else:
    print(k)

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

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 mips():
    return map(int,input().split())
def msps():
    return map(str,input().split())

import sys
A,B,C,K = mips()
ans = 0
reste = K
if A >= K:
    print(K)
    sys.exit(0)
if A < K:
    ans += A
    reste -= A
if reste <= B:
    print(ans)
    sys.exit(0)
reste -= B
if reste <= C:
    ans -= reste
    print(ans)
else:
    ans -= C
    print(ans)

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

Solution (Python):
def task(a, b, c, k):
    pluses = min(a, k)
    zeros = min(b, k - pluses)
    minuses = min(c, k - pluses - zeros)

    total = pluses - minuses
    print(total)


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

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


if __name__ == "__main__":
    main()

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

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

print(cnt)

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

Solution (Python):
def wa(a,b,c,k):
    if k <= a:
        print(k)
    elif a < k <= b:
        print(a)
    else:
        print(a-(k-a-b))

a,b,c,k = [int(i) for i in input().split()]
wa(a,b,c,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:
    print(2*A-(K-B))

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

def main(A, B, C, K):
  point = 0
  if K<=A:
    return K
  point += A
  K -= A
  if K<=B:
    return point
  K -= B
  return point-K

print(main(A, B, C, K))

Solution (Python):
import math,sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda:int(input())
li = lambda:list(map(int,input().split()))

def case():
    a,b,c,k=li()
    ans=0
    if(k>=a):
        ans+=a
        k-=a 
    else:
        ans=k
        k=0
   
    if(k>=b):
        ans+=0
        k-=b 
    else:
        k=0 
    if(k>=c):
        ans-=c
    else:
        ans-=k 
    print(ans)

for _ in range(1):
    case()

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

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)*-1))
else:
    print(a - c)

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

m = 0


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

print(m)

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

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

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

summ = min(K, A)

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

print(ans)

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

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

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

ans = 0

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

Solution (Python):
import sys


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


solve()

Solution (Python):




import sys
input = sys.stdin.readline



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

    res = 0

    if A >= K:
        res += K
        K = 0
    else:
        res += A
        K -= A

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

    if C >= K:
        res -= K
        K = 0
    else:
        res -= A
        K -= C

    print(res)


main()

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):
a,b,c,k=map(int,input().split())
ans=0
zan=k
if k<=a:
    ans+=1*k
else:
    ans+=1*a
    zan-=a
    if zan<=b:
        zan=zan
    else:
        zan-=b
        ans-=1*zan
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())
ans = 0
ans += 1*( min(a, k) )
k -= min(a, k)
ans += 0*( min(b, k) )
k -= min(b, k)
ans += (-1)*( min(c, k) )
k -= min(c, k)
print(ans)

Solution (Python):




import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

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

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

    
    
    
    

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

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

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

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

ans = 0

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

else:
    print(a+b+c)

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 = 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()))
res = min(a, k)
f = True
k -= min(a, k)
if k == 0:
    print(res)
    f = False
k -= min(b, k)
if k == 0 and f:
    print(res)
    f = False
res -= min(c, k)
if f:
    print(res)

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

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

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

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

n = 0
if k >= a:
    n += a
    k -= a
else:
    n = k
    k = 0

k -= b

if k >= 0:
    n -= max(k, 0)
print(n)

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

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

n = min(b, k)
k -= n

n = min(c, k)
ans -= n

print(ans)

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

Solution (Python):

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

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

print(res)

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

ans = a
k = k - a

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

k = k - b
ans = ans - k
print(ans)

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

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 = (int(x) for x in input().split())
ans = 0
num = 0
if k<=a:
    print(k)
else:
    if a+b>=k:
        print(a)
    else:
        print(2*a-k+b)

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

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

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

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

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

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

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

print(ans)

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

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

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

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

Solution (Python):
inp=input
li=list
iinp=lambda : int(inp())
ent=lambda : map(int,inp().split())
lient=lambda : [int(i) for i in inp().split()]
li0= lambda x: [0 for i in range(x)]
livalor=lambda valor,x: [valor for i in range(x)]
stent=lambda : [i for i in inp()]
filldp=lambda valor,x,y: [[valor for i in range(y)] for i in range(x)]

a,b,c,k=ent()
res=a if a<=k else k
k=k-a
if k<=0:
    print(res)
else:
    k=k-b
    if k<=0:
        print(res)
    else:
        res=res-k if k<=c else res-c
        print(res)

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


def main():
    A, B, C, K = readnums()
    if A > K:
        print(K)
    elif A + B >= K:
        print(A)
    elif A + B < K:
        print(A - (K - (A + B)))


if __name__ == "__main__":
    main()

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

res=0
res+=min(k,a)
k-=min(k,a)

if k>0:
    k-=min(b,k)

if k>0:
    res-=min(k,c)
    k-=min(k,c)

print(res)

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

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    a, b, c, k = LI()
    num = 0
    if k < a:
        num = k
        print(num)
        return

    k -= a
    num += a
    if k < b:
        print(num)
        return
    k -= b

    if k < c:
        num -= k
        print(num)
        return
    k -= c
    num -= c
    print(num)
main()

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

Solution (Python):
a,b,c,k = map(int,input().split())
sum1 = 0
if k <= a:
    print(k)
else:
    sum1 += a
    k -= a
    if k <= b:
        print(sum1)
    else:
        k -= b
        if k <= c:
            print(sum1 - 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):
import bisect,collections,copy,heapq,itertools,math,string
import sys
def S(): return sys.stdin.readline().rstrip()
def M(): return map(int,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
A, B, C, K = M()
ans = 0
if A < K:
    ans += A
    K -= A
    if B < K:
        K -= B
        ans -= K
else:
    ans = K
print(ans)

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

point = 0

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

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

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

Solution (Python):
a=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(a[0]-(a[3]-a[0]-a[1]))

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

Solution (Python):
def main():
    a,b,c,k = map(int, input().split())
    if a > k:
        ans = k
    elif a + b < k:
        c_n = k - (a+b)
        ans = a - c_n
    else:
        ans = a
    print(ans)
if __name__ == '__main__':
    main()

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

Solution (Python):

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

ans = 0

if K >= A:
    ans += A
    K -= A
else:
    ans += K
    K = 0

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

if K >= C:
    ans -= C
    K -= C
else:
    ans -= K
    K = 0

print (ans)

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

if K <= A + B:
    print(min(K, A))
else:
    numC = K - A - B
    print(A - numC)

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

ans = 0

if A - K < 0:
    ans += A
    K -= A
    if B - K < 0:
        K -= B
        if C - K < 0:
            print(ans - C)
        else:
            print(ans - K)
    else:
        print(ans)
else:
    print(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())
if (a >= k):
    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())
cnt = 0
if a <= k:
    cnt += a
    k -= a
    if b <= k:
        k -= b
        if k > 0:
            cnt -= k

else:
    cnt += k
    

print(cnt)

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

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

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(k if a>=k else a if a+b>=k else 2*a+b-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,B,C,K = (int(x) for x in input().split())
ans = 0
if(A>=K):
    ans = K
elif(A+B>=K):
    ans = A
elif(A+B+C>=K):
    ans = A - (K-A-B)
print(ans)

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

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

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

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

Solution (Python):
from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

import os, sys, bisect, copy
from collections import defaultdict, Counter, deque

if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')

def input(): return sys.stdin.readline()
def mapi(arg=0): return map(int if arg==0 else str,input().split())


a,b,c,k = mapi()
res = 0
res,k = min(k,a),max(0,k-min(k,a))
k = max(k-min(k,b),0)
res-=min(k,c)
print(res)

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

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

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

print(a-k)

Solution (Python):
def main():
    a, b, c, k = (int(i) for i in input().split())
    if k < a:
        ans = k
    elif k == a:
        ans = a
    elif a < k and k <= a+b:
        ans = a
    elif a+b < k:
        ans = a
        k -= a+b
        ans -= k
    print(ans)


if __name__ == '__main__':
    main()

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

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

A, B, C, K = map(int, sys.stdin.readline().strip().split())

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

print(a - c)

Solution (Python):

import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left 
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

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

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

abc167_b()

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

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

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

ans = 0

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


print(ans)

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

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

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

Solution (Python):

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

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

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

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

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

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

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

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

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

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

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

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


print(a-c)

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

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

Solution (Python):

import math

def input_num():
	ls = input().strip().split(" ")
	return [int(e) for e in ls]

a,b,c,k = input_num()

b_n, c_n = 0,0
a_n = min(k,a)
k -= a_n
if k > 0:
	b_n = min(k,b)
	k -= b_n
	if k > 0:
		c_n = k
print(a_n-c_n)

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

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

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 = [int(x) for x in input().split()]

ans = 0

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

Solution (Python):
def main():
    from sys import stdin
    a,b,c,k = map(int,stdin.readline().rstrip().split())
    if k <=a:
        print(k)
    elif k<=a+b:
        print(a)
    else:
        print(a-(k-a-b))
main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

""" Input
S = input().rstrip()
N = int(input())
N, A, B = map(int, input().split())
A = list(map(int, input().split()))
A = np.array(input().split(), dtype=np.float64)
D = [int(input()) for _ in range(N)]
AB = [[int(x) for x in input().split()] for _ in range(N)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
"""

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

total = 0
if K >= A:
    total += 1 * A
    K -= A
    if K >= B:
        total += 0 * B
        K -= B
        if K >= C:
            total += (-1) * C
        else:
            total += (-1) * K
    else:
        pass

else:
    total = K * 1

print(total)

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

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

ans = 0

ans += min(a, k)
k -= a
k -= b
if k <= 0:
    print(ans)
    exit()
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 K <= A:
    print(K)
elif K <= A+B:
    print(A)
else:
    minus = K-A-B
    print(A-minus)

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

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