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 random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)


inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""

from collections import deque
import sys
sys.setrecursionlimit(10 ** 7)


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



def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


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


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def floor(a, b):
    """floor(a/b) 整数値を返す"""
    c = (a - a % b)/b
    return int(c)


def main():

    a, b, c, k = wi()
    ans = 0
    d = k - a
    e = d - b
    f = e - c
    if d >= 0:
        ans += a
        if e >= 0 and f >= 0:
            ans -= c
            print(ans)
        elif e >= 0 and f < 0:
            ans -= e
            print(ans)
        else:
            print(ans)
    else:
        ans += k
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
A, B, C, K = (int(i) for i in input().split())  
ans = 0

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

Solution (Python):

import sys


def solve(A: int, B: int, C: int, K: int):
    if K <= A:
        print(K)
    elif A < K <= A+B:
        print(A)
    elif A+B < K:
        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 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 == 0:
  print(0)
elif A >= K:
  print(K)
elif A + B >= K:
  print(A)
elif A + B + C >= K:
  print(A - (K -(A + B)))
elif K == 0:
  print(0)

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

if a >= k:
    ans = k
else:
    ans = a
    k -= a
    if b >= k:
        pass
    else:
        k -= b
        if k != 0:
            ans -= k

print(ans)

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

count=min(a,d)
d-=a
d-=b
if d<=0:
    print(count)
else:
    print(count-d)

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

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

Solution (Python):
import numpy as np

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



res = "res"








s = a if k - a >= 0 else k

action = k - a - b

if action >= 0:
  s -= action

print(s)

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

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

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

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

Solution (Python):
a = raw_input().split()
for i in range(4):
    a[i] = int(a[i])
res = 0
b = [1, 0, -1]
t = a[3]
i = 0
while t:
    if a[i] < t:
        t -= a[i]
        res += a[i] * b[i]
        i += 1
    else:
        res += b[i] * t
        print res
        break

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

if __name__ == '__main__':
  main()

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

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

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

Solution (Python):
a = list(map(int,input().split()))
A = a[0]
B = a[1]
C = a[2]
K = a[3]
if A >= K:
  print(K)
elif A<K and B>=(K-A):
  print(A)
elif B<(K-A):
  print(A+(-1)*(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):

"""AtCoder Beginner Contest 167: B - 
https://atcoder.jp/contests/abc167/tasks/abc167_b
"""

__author__ = 'bugttle <bugttle@gmail.com>'


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

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

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

if __name__ == '__main__':
    main()

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

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

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

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

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

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

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

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

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

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

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

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
read = sys.stdin.buffer.read 
readline = sys.stdin.buffer.readline 
readlines = sys.stdin.buffer.readlines 
sys.setrecursionlimit(10**7) 

"""

n = readline().rstrip() 
lis = readline().rstrip().split() 


n = int(readline().rstrip()) 
a, b, c = map(int,readline().rstrip().split()) 
A = list(map(int,readline().rstrip().split()))  
lis = [readline().rstrip().split() for _ in range(n)] 


s_lis=[readline().rstrip().decode() for _ in range(n)] 

s_lis=["".join(sorted(readline().rstrip().decode())) for _ in range(n)]

s_lis=[readline().rstrip().split().decode() for _ in range(n)] 


s_lis=[int(readline().rstrip()) for _ in range(n)] 

s_lis=[int(readline().rstrip().split()) for _ in range(n)]


s = sys.stdin.readlines()
n, k=map(int, s[0].split())
d=[]
A=[]
for i in s[1:2*k:2]:
    d.append(int(i))
for i in s[2:2*k+1:2]:
    A.append(list(map(int, i.split())))
"""

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

if k<=a:
    print(k)

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

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

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

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

print(ans)

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

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

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

ans = 0

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

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

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

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

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



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

print(p)

Solution (Python):

from collections import deque
from operator import itemgetter
from bisect import bisect_left, bisect
import itertools
import sys
import math
import numpy as np
import time
sys.setrecursionlimit(10**6)
input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

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

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

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

if C < K:
    K -=C
    score -= C
else:
    score -= K
    print(score)
    exit()
print(score)

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

a_ = k - a
b_ = k - a - b

res = 0
if a_ <= 0:
    res += k
elif b_ <= 0:
    res += a 
else:
    res += a - b_

print(res)

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

ans = 0
x = k - a
if x < 0:
  ans += k
  print(ans)
  exit()
else:
  ans += a
  k = k - a


y = k - b
if y < 0:
  print(ans)
  exit()
else:
  k = k - b

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

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

ans = 0

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

print(ans)

Solution (Python):

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

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

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

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

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

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

    print(ans)


if __name__ == '__main__':
    main()

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

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

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

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

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

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

if a>=k or a+b>=k:
  if(k>=a):
    print(a)
  else:
    print(k)
else:
  s = k-(a+b)
  print(a-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)
else:
  rem = K-A
  if rem < B:
    print(A)
  else:
    rem = rem - B
    if rem < C:
      print(A-rem)
    else:
      print(A - C)

Solution (Python):
import sys

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

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

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

print(ans)

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

print(ans)

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

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

print(str(ans))

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

ns = lambda:stdin.readline().rstrip()
ni = lambda:int(stdin.readline().rstrip())
nm = lambda:map(int,stdin.readline().split())
nl = lambda:list(map(int,stdin.readline().split()))

A,B,C,K=nm()
ans=0

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

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

print(ans)

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

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

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

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


if __name__ == '__main__':
    main()

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())
max_ans = 0
if K <= A:
    max_ans = K
    print(max_ans)
else:
    max_ans = A
    L = K-A 
    if L <= B:
        max_ans += L*0
        print(max_ans)
    else:
        max_ans += B*0
        M = L-B
        max_ans -= M
        print(max_ans)

Solution (Python):
import sys
a, b, c, k = [int(k) for k in input().split()]
r = 0
while k > 0:
    if k >= a:
        r += a
        k -= a
    else:
        r += k
        k -= k
        continue
    if k >= b:
        k -= b
    else:
        k -= k
    if k >= c:
        r -= c
        k -= c
    else:
        r -= k
        k -= k

print(r)

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 K-A<=0:
    print(K)
    exit()
K-=A
if K-B<=0:
    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 <= 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(2*a + b - k)

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

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):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue

Mod = 1000000007
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

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

if __name__ == "__main__":
    main()

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

Max=0

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

print(Max)

Solution (Python):
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

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

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):
temp = input().split(" ")

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

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

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

res=0

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

print(res)

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


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


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


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    A, B, C, K = MAP()
    ans = min(A, K)
    K -= A
    if K > B:
        K -= B
        print(ans - K)
    else:
        print(ans)


if __name__ == '__main__':
    main()

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

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

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

print(sum - K)

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

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

Solution (Python):
from __future__ import print_function

import sys
sys.setrecursionlimit(500000)

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


















def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


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


if __name__ == '__main__':
    main()

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

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

    if (a + b) >= k:
        print(min(a, k))
    else:
        print(2 * a + b - k)
        
resolve()

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

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

if __name__ == "__main__":
    main()

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

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

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

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

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

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

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

count = 0
if K > A:
    count += A
    K -= A
    if K > B:
        K -= B
        if K > 0:
            count -= K
else:
    count += K

print(count)

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

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

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

l = []
ans = 0

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
flag = False

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

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

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

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

Solution (Python):
def main():
    A,B,C,K = map(int,input().split())
    if K<=A:
        print(K)
    else:
        summ=A
        K-=A
        if K>B:
            K-=B
            if K>0:
                summ-=K
        print(summ)
if __name__=='__main__':
    main()

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

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

Solution (Python):
a, b, c, k = [int(i) for i in 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 = 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
import math
import itertools

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

  if a > k:
    r = k
    k = 0
  else:
    r = a
    k = k - a

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

  r -= k
    

  print(r)
    

if __name__ == '__main__':
  main()

Solution (Python):
if __name__ == '__main__':
    [a,b,c,k] = [int(i) for i in input().split()]
    ans = 0
    if k >= a:
        k-=a
        ans += a
    else:
        ans += k
        k = 0
    
    if k>=b :
        k-=b
    else:
        k = 0
    
    if k>=c:
        k-=c
        ans -= c
    else:
        ans -= k
        k = 0
    
    print(ans)

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

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

ans = 0

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

print(ans)

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

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

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

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

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

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

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

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

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

if K <= A:
    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()))
tmp = K-A
if tmp>0:
  tmp -= B
  if tmp>0:
    ans = 1*A-1*(K-A-B)
  else:
    ans = 1*A
else:
  ans = 1*K
print(ans)

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

arieru_max = 0
selected = 0


if A <= K:
    arieru_max += A
    selected += A

    if B <= (K - A):
        selected += B

        if C <= (K - A - B):
            arieru_max -= C
        else:
            arieru_max -= (K - A - B)


else:
    arieru_max = K

print(arieru_max)

Solution (Python):
import sys

if __name__ == '__main__':
    p,z,n,k = list(map(lambda x: int(x), sys.stdin.readline().split(' ')))

    ans = 0
    if k > p:
        ans += p
        k -= p
    else:
        print(k)
        exit()
    if k > z:
        ans += 0
        k -= z
    else:
        print(ans)
        exit()
    ans -= k
    print(ans)

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

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

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 k:
    k -= b

if k > 0:
    ans -= k

print(ans)

Solution (Python):

import numpy as np

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

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

print(ans)

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

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

if k>=b:
    k-=b
elif k<b:
    k=0

if k>=c:
    S-=c
    k-=c
elif k<c:
    S-= k

print(S)

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

main()

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

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