Problem:
 Score : 200 points    Problem Statement We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0 s and C cards with -1 s.  We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?      Constraints  All values in input are integers.  0 \leq A, B, C  1 \leq K \leq A + B + C \leq 2 \times 10^9         Input Input is given from Standard Input in the following format:  A  B  C  K       Output Print the maximum possible sum of the numbers written on the cards chosen.        Sample Input 1 2 1 1 3      Sample Output 1 2  Consider picking up two cards with 1 s and one card with a 0 .
In this case, the sum of the numbers written on the cards is 2 , which is the maximum possible value.       Sample Input 2 1 2 3 4      Sample Output 2 0       Sample Input 3 2000000000 0 0 2000000000      Sample Output 3 2000000000

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

l = A + B + C - K

def solve(A, B, C, K):
    result = 0
    n = min(A, K)
    result += n
    K -= n
    A -= n
    if K == 0:
        return result
    n = min(B, K)
    K -= n
    B -= n
    if K == 0:
        return result
    n = min(C, K)
    result -= n
    K -= n
    C -= n
    return result

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

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

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

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

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

res = 0
d = min(a, k)
k -= d
res += d
d = min(b, k)
k -= d
d = min(c, k)
res -= d
print(res)

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

Solution (Python):
def main():
    A,B,C,K=map(int,input().split())
    print(min(A, K) - max(0, K-A-B))
    return

if __name__=='__main__':
    main()

Solution (Python):
def main():
    A,B,_,K=map(int,input().split())
    print(min(A, K) - max(0, K-A-B))
    return

if __name__=='__main__':
    main()

Solution (Python):
def main():
    A,B,_,K=map(int,input().split())
    print(min(A, K) - max(0, K-A-B))
    return

if __name__=='__main__':
    main()

Solution (Python):
def main():
    A,B,C,K=map(int,input().split())
    print(min(A, K) - max(0, K-A-B))
    return

if __name__=='__main__':
    main()

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

if __name__=='__main__':
    main()

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

ak = min(a, k)
k -= ak
if k == 0:
    ans = ak
    print (ans)
else:
    bk = min(b, k)
    k -= bk
    if k == 0:
        ans = ak
        print (ans)
    else:
        ck = k
        ans = ak- ck
        print (ans)

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

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    a,b,c,k=MI()
    ans=0
    if a>=k:
        ans=k
    else:
        ans=a
        k-=a
        if b>=k:
            pass
        else:
            k-=b
            ans-=k
            
    print(ans)

main()

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

ans=0
n=k

x=min(a,n)
ans+=x
n-=x

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

z=min(c,n)*-1
ans+=z

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

def inputlist(): return [int(k) for k in input().split()]

A,B,C,K = inputlist()
if A >= K:
    print(K)
    exit()
if A +B >= K:
    print(A)
    exit()
print(2*A-K+B)

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

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import itertools
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())

A, B, C, K = inpl()
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 A >= K:
	print(K)
	exit()
K -= A
if B >= K:
	print(A)
	exit()
K -= B
print(A - K)

Solution (Python):
import sys
if __name__ == "__main__":
    a, b, c, k = map(int, input().split())
    ans = 0

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

    ans -= k
    print(ans)

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

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

if k <= a:
    sum = k * 1
elif a < k and k <= a + b:
    sum = a
elif a + b < k <= a + b + c:
    sum = a + (-1) * (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 = map(int, input().split())

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

Solution (Python):
from sys import stdin
A, B, C, K = [int(_) for _ in stdin.readline().rstrip().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())

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

Solution (Python):
INPUT = input()
A = ''
B = ''
C = ''
K = ''
count = 0

for i in range(len(INPUT)):
    if count == 0:
        if not INPUT[i] == ' ':
            A += str(INPUT[i])
        else:
            count += 1
    elif count == 1:
        if not INPUT[i] == ' ':
            B += str(INPUT[i])
        else:
            count += 1
    elif count == 2:
        if not INPUT[i] == ' ':
            C += str(INPUT[i])
        else:
            count += 1
    elif count == 3:
            K += str(INPUT[i])

A = int(A)
B = int(B)
C = int(C)
K = int(K)
answer = 0



if A >= K:
    answer = K
else:
    answer = A
    K = K - A
    if B >= K:
        pass
    else:
        K = K - B
        answer = answer - K

print(answer)

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

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

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

print(ans)

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

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

total = 0

if k > a:
    k = k-a
    total += a

elif k == a:
    k = 0
    total = a

elif k < a:
    total = k
    k = 0

if k > b:
    k = k-b

elif k == b:
    k = 0

elif k < b:
    k = 0

if k > c:
    total -= c
    k -= c

elif k == c:
    total -= k
    k = 0

elif k < c:
    total -= k
    k = 0
    
print(total)

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

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

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

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


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

Solution (Python):
ls = list(map(int, input().split()))
nokori = ls[-1]
nokori_ = ls[-1]
sum_max = 0
for i, j in zip(ls[:-1], [1, 0, -1]):
    nokori -= i
    if nokori > 0:
        sum_max += i*j
    elif nokori == 0:
        sum_max += i*j
        break
    else:
        sum_max += nokori_*j
        break
    nokori_ = nokori
print(sum_max)

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

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

Solution (Python):

def isInt(x):
    try:
        float(x)
    except ValueError:
        return False
    else:
        return float(x).is_integer()
    
def main():
    try:
        str = input().split()
    except ValueError:
        return

    
    if isInt(str[0]):
        a = int(str[0])
    else:
        return
    if isInt(str[1]):
        b = int(str[1])
    else:
        return
    if isInt(str[2]):
        c = int(str[2])
    else:
        return
    if isInt(str[3]):
        k = int(str[3])
    else:
        return

    
    if not(a >= 0 and b >= 0 and c >=0):
        return

    
    if not(1 <= k and k <= a+b+c and a+b+c <= 2 * 10 ** 9):
        return

    sum = None
    if(k <= a):
        sum = k
        return sum
    else:
        sum = a
        k -= a

    if(k <= b):
        return sum
    else:
        k -= b
        sum -= k
        return sum
        
if __name__ == '__main__':
    sum = main()
    if(isInt(sum)):
        print(sum)

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

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

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

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

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

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

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

def k(n, m):
    global ans, K
    if K >= n:
        K -= n
        ans += (n * m)
    else:
        ans += (K * m)
        K = 0
        
k(A, 1)
k(B, 0)
k(C, -1)

print(ans)

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

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

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

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

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

Solution (Python):
n=list(map(int,input().split()))
s=0
k=n[3]
a=n[0]
b=n[1]
d=k-a

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

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

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

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

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

if k>=a:
    aa=a
    k=k-a;
    if k>=b:
        bb=b
        k=k-b
        cc=k
    else:
        bb=k
        k=0
else:
    aa=k
    k=0

print(aa-cc)

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

a = i[0]
b = i[1]
c = i[2]
d = i[3]

if int(d) <= int(a):
  print(int(d))
elif int(d) - int(a) <= int(b):
    print(int(a))
else:
  print(int(a) - (int(d) - int(a) -int(b)))

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

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

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

    k -= a

    if k > 0:
        k -= b

    if k > 0:
        ans -= k

    print(ans)


if __name__ == '__main__':
    main()

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

ans=0

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

k-=a

if b>=k:
  print(a)
  exit()

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

cards = k
sum = 0

while (True):
    if cards > a:
        sum += a
        cards -= a
        if cards > b:
            sum += 0
            cards -= b
            if cards > c:
                sum -= c
                cards -= b
            else:
                sum -= cards
                print(sum)
                break
        else:
            print(sum)
            break

    else:
        sum += cards
        print(sum)
        break

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

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

Solution (Python):

import sys


def main():
    A, B, C, K = map(int, input().split())
    score = min(K, A) * 1
    K -= A + B
    if K > 0:
        score -= K
    print(score)
if __name__ == '__main__':
    main()

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

if k>a:
    m+=a
    k-=a
    if k>b:
        k-=b
        if k>c:
            m-=c
        else:
            m-=k
else:
    m=k


print(m)

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

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

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

Solution (Python):
from itertools import combinations
def main():
    a,b,c,k=map(int,input().split())
    if k<=a:
        print(k)
        return
    if k<=a+b:
        print(a)
        return
    print(a-(k-a-b))

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











arg = list(map(int, input().split())) 

A = arg[0]
B = arg[1]
C = arg[2]
K = arg[3]

X = 0

if K <= A:
  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())

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

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

def solve(A,B,C,K):
    remain=K
    answer=0
    answer+=1*min(remain,A)
    remain-=A
    if remain<=0:
        return answer
    remain-=B
    if remain<=0:
        return answer
    answer-=remain
    return answer

out=solve(A,B,C,K)
print(out)

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())
a = min(A,K)
K -= a
b = min(B,K)
K -= b
c = min(C,K)
K -= c

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

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

summ = 0

if k <= a:
    summ = k

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

print(summ)

Solution (Python):
a,b,c,k = list(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())
print(min(a, k) + min(b, max(0, k-a)) * 0 - min(c, max(0, k-a-b)))

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

if K >= A:
    result = A
    K -= A
else:
    result = K
    K = 0

if K >= B:
    K -= B

if K > 0:
    result -=K
print(result)

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

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

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

Solution (Python):
from sys import stdin, stdout
import math,sys
from itertools import permutations, combinations
from collections import defaultdict,deque,OrderedDict
import bisect as bi
import heapq 

'''

 
def I():return (int(stdin.readline()))
def In():return(map(int,stdin.readline().split()))
'''

 

def I():return (int(input()))
def In():return(map(int,input().split()))



def main():
    try:
        a,b,c,d=In()
        if a>=d:
            print(d)
        elif a+b>=(d):
            print(a)
        elif a+b+c>=d:
            a-=(d-b-a)
            print(a)
        else:
            print(a-c)
    except:
        pass
        
M = 998244353
P = 1000000007
 
if __name__ == '__main__':
    for _ in range(1):
        main()

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

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

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

    if K<=A:
        cal=K
        print(cal)
        return

    cal+=A
    K-=A
    if K<=B:
        print(cal)
        return

    K-=B
    print(cal-K)
    return


main()

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

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

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

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

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

print(total)

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

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

Solution (Python):
inp = list(map(int,input().split()))
res = 0

if inp[3] <= inp[0] :
    res = inp[3]
elif inp[3] <= (inp[0] + inp[1]) :
    res = inp[0]
elif inp[3] < (inp[0] + inp[1] + inp[2]) :
    res = inp[0] - (inp[3] - inp[0] - inp[1])
elif inp[3] == (inp[0] + inp[1] + inp[2]) :
    res = inp[0] - inp[2] 

print(res)

Solution (Python):
def linear_program(A, B, K):
    if K <= A:
        return K
    elif K <= A+B:
        return A
    else:
        K -= A+B
        return A-K

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

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

x_a = min(A,K)
K -= x_a

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

x_c = min(C,K)
K -= x_c

print(x_a - x_c)

Solution (Python):
import sys

a = list(map(int,input().split()))

if a[3] > a[0]:
    b = a[0]
    a[3] = a[3]-a[0]
elif a[3] <= a[0]:
    b = a[3]
    print(b)
    sys.exit()

if a[3] > a[1]:
    a[3] = a[3] - a[1]
elif a[3] <= a[1]:
    print(b)
    sys.exit()
c = (-1) * a[3]
b = c + b
print(b)

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())
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)
elif 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-=(a+b)
ans-=max(k,0)
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, b, c, k = map(int,input().split())

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

print(ans)

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):
import math
import time
from collections import defaultdict,deque
from sys import stdin,stdout
from bisect import bisect_left,bisect_right
a,b,c,k=map(int,stdin.readline().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 * 1
    K = K - A
else:
    S += K * 1
    K = 0

if K - B > 0:
    S += B * 0
    K = K - B
else:
    S += K * 0
    K = 0

if K - C > 0:
    S += C * -1
    K = K - C
else:
    S += K * -1
    K = 0

print(S)

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



'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

a, b, c, k = map(int, 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())
ans = min(a, k) - max(0, k-a-b)
print(ans)

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

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

Solution (Python):


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

k=d

if(a-k>0):

  print(k)

else:

  k=d-a

  if(b-k>0):

    print(a)

    

  else:

    k=d-a-b

    print(a-k)

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

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

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

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

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

sum = xa * (1) + xb * (0) + xc * (-1)
print(sum)

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

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

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

cards = 0
ans = 0

if A >= K:
    ans = K
else:
    ans += A
    cards += A
    if cards + B >= K:
        pass
    else:
        cards += B
        ans +=  -1 * (K - cards)

print(ans)

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

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

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

Solution (Python):
abck = input().split(" ")
a = int(abck[0])
b = int(abck[1])
k = int(abck[3])

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

Solution (Python):
abck = [int(s) for s in input().split()]
a, b, c, k = abck[0], abck[1], abck[2], abck[3]
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 A >= K:
    print(1*K)

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

Solution (Python):
a, b, c, k = map(int, input().split())
p = min(a, k)
s = p
k -= p
 
q = min(b, k)
k -= q
s -= k
 
r = min(c, k)
k -= r
s -= k
print(s)

Solution (Python):
def test():
    A, B, C, K = map(int, input().split())
    A_K_MIN = min(A, K)
    ans = A_K_MIN
    K -= A_K_MIN

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

    ans -= K
    print(ans)


if __name__ == "__main__":
    test()

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

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

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

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

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

mn = min(C,K)
K -= mn
ans -= mn
print(ans)

Solution (Python):

a,b,c,k = map(int,input().split())
if 0<=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()))
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())
list1 = [a,b,c]
ans=0

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

print("{}".format(ans))

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

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

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

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

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

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

Solution (Python):

import sys


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



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  
    B = int(next(tokens))  
    C = int(next(tokens))  
    K = int(next(tokens))  
    solve(A, B, C, K)

if __name__ == '__main__':
    main()

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

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

if __name__ == "__main__":
    main()

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

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

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


if __name__ == "__main__":
    main()

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

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

Solution (Python):


A, B, C, K=map(int, input().split())
if A>K:
    print("{}".format(K))
else:
    if A+B>=K:
        print("{}".format(A))
    else:
        print("{}".format(A-(K-A-B)))

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

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