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):
def modinv(x, mod):
    a, b = x, mod
    u, v = 1, 0
    while b:
        t = a // b
        a -= t * b; a, b = b, a
        u -= t * v; u, v = v, u
    return u % mod


def _garner(xs, mods):
    M = len(xs)
    coeffs = [1] * M
    constants = [0] * M
    for i in range(M - 1):
        mod_i = mods[i]
        v = (xs[i] - constants[i]) * modinv(coeffs[i], mod_i) % mod_i
        for j in range(i + 1, M):
            mod_j = mods[j]
            constants[j] = (constants[j] + coeffs[j] * v) % mod_j
            coeffs[j] = (coeffs[j] * mod_i) % mod_j

    return constants[-1]


def bit_reverse(d):
    n = len(d)
    ns = n >> 1
    nss = ns >> 1
    ns1 = ns + 1
    i = 0
    for j in range(0, ns, 2):
        if j < i:
            d[i], d[j] = d[j], d[i]
            d[i + ns1], d[j + ns1] = d[j + ns1], d[i + ns1]
        d[i + 1], d[j + ns] = d[j + ns], d[i + 1]
        k = nss
        i ^= k
        while k > i:
            k >>= 1
            i ^= k
    return d


def _ntt(a, sign, mod, g):
        n = len(a)
        tmp = (mod - 1) * modinv(n, mod) % mod  
        h = pow(g, tmp, mod)  
        if sign < 0:
            h = modinv(h, mod)

        a = bit_reverse(a)

        m = 1
        while m < n:
            m2 = m << 1
            _base = pow(h, n // m2, mod)
            _w = 1
            for x in range(m):
                for s in range(x, n, m2):
                    u = a[s]
                    d = a[s + m] * _w % mod
                    a[s] = (u + d) % mod
                    a[s + m] = (u - d) % mod
                _w = _w * _base % mod
            m <<= 1
        return a

def ntt(a, mod, g):
        return _ntt(a, 1, mod, g)
  
def intt(a, mod, g):
        n = len(a)
        n_inv = modinv(n, mod)
        a = _ntt(a, -1, mod, g)
        for i in range(n):
            a[i] = a[i] * n_inv % mod
        return a
      
def convolution(a, b, mod, g):
        ret_size = len(a) + len(b) - 1
        n = 1 << (ret_size - 1).bit_length()
        _a = a + [0] * (n - len(a))
        _b = b + [0] * (n - len(b))
        _a = ntt(_a, mod, g)
        _b = ntt(_b, mod, g)
        _a = [x * y % mod for x, y in zip(_a, _b)]
        _a = intt(_a, mod, g)
        _a = _a[:ret_size]
        return _a


def convolution_ntt(a, b, mod):
    a = [x % mod for x in a]
    b = [x % mod for x in b]

    mods = (167772161, 469762049, 1224736769, mod)

    ntt1 = NTT(mods[0], 3)
    ntt2 = NTT(mods[1], 3)
    ntt3 = NTT(mods[2], 3)

    x1 = ntt1.convolution(a, b)
    x2 = ntt2.convolution(a, b)
    x3 = ntt3.convolution(a, b)

    n = len(x1)
    ret = [0] * n
    for i in range(n):
        xs = [x1[i], x2[i], x3[i], 0]
        ret[i] = _garner(xs, mods)

    return ret

N = 100000
A = []
B = []
s = 0
for i in range(N):
    s = (s * 3 + i * 7) % 100
    a = s
    b = (s * 3 + a * 11) % 100
    A.append(a)
    B.append(b)

AB = convolution(A, B, 998244353, 5)



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

Solution (Python):
def modinv(x, mod):
    a, b = x, mod
    u, v = 1, 0
    while b:
        t = a // b
        a -= t * b; a, b = b, a
        u -= t * v; u, v = v, u
    return u % mod


def _garner(xs, mods):
    M = len(xs)
    coeffs = [1] * M
    constants = [0] * M
    for i in range(M - 1):
        mod_i = mods[i]
        v = (xs[i] - constants[i]) * modinv(coeffs[i], mod_i) % mod_i
        for j in range(i + 1, M):
            mod_j = mods[j]
            constants[j] = (constants[j] + coeffs[j] * v) % mod_j
            coeffs[j] = (coeffs[j] * mod_i) % mod_j

    return constants[-1]


def bit_reverse(d):
    n = len(d)
    ns = n >> 1
    nss = ns >> 1
    ns1 = ns + 1
    i = 0
    for j in range(0, ns, 2):
        if j < i:
            d[i], d[j] = d[j], d[i]
            d[i + ns1], d[j + ns1] = d[j + ns1], d[i + ns1]
        d[i + 1], d[j + ns] = d[j + ns], d[i + 1]
        k = nss
        i ^= k
        while k > i:
            k >>= 1
            i ^= k
    return d


class NTT:
    def __init__(self, mod, primitive_root):
        self.mod = mod
        self.root = primitive_root

    def _ntt(self, a, sign):
        n = len(a)
        mod, g = self.mod, self.root
        tmp = (mod - 1) * modinv(n, mod) % mod  
        h = pow(g, tmp, mod)  
        if sign < 0:
            h = modinv(h, mod)

        a = bit_reverse(a)

        m = 1
        while m < n:
            m2 = m << 1
            _base = pow(h, n // m2, mod)
            _w = 1
            for x in range(m):
                for s in range(x, n, m2):
                    u = a[s]
                    d = a[s + m] * _w % mod
                    a[s] = (u + d) % mod
                    a[s + m] = (u - d) % mod
                _w = _w * _base % mod
            m <<= 1
        return a

    def ntt(self, a):
        return self._ntt(a, 1)

    def intt(self, a):
        mod = self.mod
        n = len(a)
        n_inv = modinv(n, mod)
        a = self._ntt(a, -1)
        for i in range(n):
            a[i] = a[i] * n_inv % mod
        return a

    def convolution(self, a, b):
        mod = self.mod
        ret_size = len(a) + len(b) - 1
        n = 1 << (ret_size - 1).bit_length()
        _a = a + [0] * (n - len(a))
        _b = b + [0] * (n - len(b))
        _a = self.ntt(_a)
        _b = self.ntt(_b)
        _a = [x * y % mod for x, y in zip(_a, _b)]
        _a = self.intt(_a)
        _a = _a[:ret_size]
        return _a


def convolution_ntt(a, b, mod):
    a = [x % mod for x in a]
    b = [x % mod for x in b]

    mods = (167772161, 469762049, 1224736769, mod)

    ntt1 = NTT(mods[0], 3)
    ntt2 = NTT(mods[1], 3)
    ntt3 = NTT(mods[2], 3)

    x1 = ntt1.convolution(a, b)
    x2 = ntt2.convolution(a, b)
    x3 = ntt3.convolution(a, b)

    n = len(x1)
    ret = [0] * n
    for i in range(n):
        xs = [x1[i], x2[i], x3[i], 0]
        ret[i] = _garner(xs, mods)

    return ret

ntt = NTT(998244353, 5)
N = 100000
A = []
B = []
s = 0
for i in range(N):
    s = (s * 3 + i * 7) % 100
    a = s
    b = (s * 3 + a * 11) % 100
    A.append(a)
    B.append(b)

AB = ntt.convolution(A, B)



A, B, C, K = map(int, 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 k <= a + b:
    print(a)
else:
    print(2 * a + b - k)

Solution (Python):
from numba import njit
 

@njit()
def convolve(a, b):
    def poww(aa, bb, mm):
        ss = 1
        tt = aa
        while bb:
            if bb % 2:
                ss = ss * tt % mm
                bb -= 1
            tt = tt * tt % mm
            bb //= 2
        return ss
    def fft(n, p, g, w, invw, f):
        d = n // 2
        v = w
        while d >= 1:
            u = 1
            for i in range(d):
                for j in range(i, n, 2*d):
                    f[j], f[j+d] = (f[j] + f[j+d]) % p, u * (f[j] - f[j+d]) % p
                u = u * v % p
            v = v * v % p
            d //= 2
 
    def ifft(n, p, g, w, invw, f):
        d = 1
        while d < n:
            v = poww(invw, n // (2 * d), p)
            u = 1
            for i in range(d):
                for j in range(i, n, 2*d):
                    f[j+d] *= u
                    f[j], f[j+d] = (f[j] + f[j+d]) % p, (f[j] - f[j+d]) % p
                u = u * v % p
            d *= 2
 
    p = 1107296257
    g = 5
    n0, n1 = len(a), len(b)
    n = 1
    while n < max(n0, n1) * 2:
        n *= 2
    a = a + [0] * (n-n0)
    b = b + [0] * (n-n1)

    w = poww(g, (p - 1) // n, p)
    invw = poww(w, p-2, p)
    fft(n, p, g, w, invw, a), fft(n, p, g, w, invw, b)
    for i in range(n):
        a[i] = a[i] * b[i] % p
    ifft(n, p, g, w, invw, a)
    invn = poww(n, p - 2, p)
    for i in range(n0 + n1 - 1):
        a[i] = a[i] * invn % p
    return a[:n0 + n1 - 1]

N = 100000
A = []
B = []
s = 0
for i in range(N):
    s = (s * 3 + i * 7) % 100
    a = s
    b = (s * 3 + a * 11) % 100
    A.append(a)
    B.append(b)

AB = convolve(A, B)



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

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    a,b,c,k = LI()
    r = min(a,k)
    k -= a
    k -= b
    if k > 0:
        r -= k

    return r

print(main())

Solution (Python):

 

def convolve(a, b):
    def poww(aa, bb, mm):
        ss = 1
        tt = aa
        while bb:
            if bb % 2:
                ss = ss * tt % mm
                bb -= 1
            tt = tt * tt % mm
            bb //= 2
        return ss
    def fft(n, p, g, w, invw, f):
        d = n // 2
        v = w
        while d >= 1:
            u = 1
            for i in range(d):
                for j in range(i, n, 2*d):
                    f[j], f[j+d] = (f[j] + f[j+d]) % p, u * (f[j] - f[j+d]) % p
                u = u * v % p
            v = v * v % p
            d //= 2
 
    def ifft(n, p, g, w, invw, f):
        d = 1
        while d < n:
            v = poww(invw, n // (2 * d), p)
            u = 1
            for i in range(d):
                for j in range(i, n, 2*d):
                    f[j+d] *= u
                    f[j], f[j+d] = (f[j] + f[j+d]) % p, (f[j] - f[j+d]) % p
                u = u * v % p
            d *= 2
 
    p = 1107296257
    g = 5
    n0, n1 = len(a), len(b)
    n = 1
    while n < max(n0, n1) * 2:
        n *= 2
    a = a + [0] * (n-n0)
    b = b + [0] * (n-n1)

    w = poww(g, (p - 1) // n, p)
    invw = poww(w, p-2, p)
    fft(n, p, g, w, invw, a), fft(n, p, g, w, invw, b)
    for i in range(n):
        a[i] = a[i] * b[i] % p
    ifft(n, p, g, w, invw, a)
    invn = poww(n, p - 2, p)
    for i in range(n0 + n1 - 1):
        a[i] = a[i] * invn % p
    return a[:n0 + n1 - 1]

N = 100000
A = []
B = []
s = 0
for i in range(N):
    s = (s * 3 + i * 7) % 100
    a = s
    b = (s * 3 + a * 11) % 100
    A.append(a)
    B.append(b)

AB = convolve(A, B)



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

Solution (Python):
from numba import njit
 
@njit(cache=True)
def convolve(a, b):
    def poww(aa, bb, mm):
        ss = 1
        tt = aa
        while bb:
            if bb % 2:
                ss = ss * tt % mm
                bb -= 1
            tt = tt * tt % mm
            bb //= 2
        return ss
    def fft(n, p, g, w, invw, f):
        d = n // 2
        v = w
        while d >= 1:
            u = 1
            for i in range(d):
                for j in range(i, n, 2*d):
                    f[j], f[j+d] = (f[j] + f[j+d]) % p, u * (f[j] - f[j+d]) % p
                u = u * v % p
            v = v * v % p
            d //= 2
 
    def ifft(n, p, g, w, invw, f):
        d = 1
        while d < n:
            v = poww(invw, n // (2 * d), p)
            u = 1
            for i in range(d):
                for j in range(i, n, 2*d):
                    f[j+d] *= u
                    f[j], f[j+d] = (f[j] + f[j+d]) % p, (f[j] - f[j+d]) % p
                u = u * v % p
            d *= 2
 
    p = 1107296257
    g = 5
    n0, n1 = len(a), len(b)
    n = 1
    while n < max(n0, n1) * 2:
        n *= 2
    a = a + [0] * (n-n0)
    b = b + [0] * (n-n1)

    w = poww(g, (p - 1) // n, p)
    invw = poww(w, p-2, p)
    fft(n, p, g, w, invw, a), fft(n, p, g, w, invw, b)
    for i in range(n):
        a[i] = a[i] * b[i] % p
    ifft(n, p, g, w, invw, a)
    invn = poww(n, p - 2, p)
    for i in range(n0 + n1 - 1):
        a[i] = a[i] * invn % p
    return a[:n0 + n1 - 1]

N = 100000
A = []
B = []
s = 0
for i in range(N):
    s = (s * 3 + i * 7) % 100
    a = s
    b = (s * 3 + a * 11) % 100
    A.append(a)
    B.append(b)

AB = convolve(A, B)



A, B, C, K = map(int, 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())
d=1
sum=0
if a>=k:
  print(k)
elif a<k:
  if a+b>=k:
    print(a)
  else:
    sa=k-(a+b)
    print(a-sa)

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

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

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

Solution (Python):
A, B, C, K = map(int, input().split())
if A >= K:
    a = K
    b = 0
    c = 0
else:
    a = A
    if B >= K - a:
        b = K - a
        c = 0
    else:
        b = B
        c = K - a - b
print("{}".format(a-c))

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

a=int(a)
b=int(b)
c=int(c)
k=int(k)

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

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

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

a = int(a)
b = int(b)
c = int(c)
k = int(k)


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

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

print(ans - k)

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

ans=0

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

ans = 0

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

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

ans = 0
if k > a and k-a > b:
    ans = a + (k - a - b) * -1
elif k >= a:
    ans = a
else:
    ans = k

print(ans)

Solution (Python):
string = input()
array = string.split(' ')
a = int(array[0])
b = int(array[1])
c = int(array[2])
k = int(array[3])

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

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

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

u = min(A, K)
v = max(0, K - A - B)
print(u - v)

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

print(ans)

Solution (Python):
A, B, C, K = (int(x) for x in 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))
elif A + B + C <= K:
  print(A-C)

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

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

if a == d:
  print(a)
elif a > d:
  print(d)
else:
  ans = a
  d -= a
  if b >= d:
    print(ans)
  else:
    d -= b
    print(ans - d)

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



card_num = 0 
card_sum = 0 


if k <= a:
    card_sum = k

elif k <= a + b:
    card_sum = a

else:
    card_sum = a - (k-a-b)

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

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

Solution (Python):

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

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

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

print(min(A, K)-max(0, K-A-B))

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

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

K = max(0, K-B)
result -= K

print(result)

Solution (Python):



ans = 0
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())
sum = 0


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

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

if A >= K:
    s += K
    K -= K
else:
    s += A
    K -= A

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

if C >= K:
    s -= K
    K -= K
else:
    s += C
    K -= C

print(s)

Solution (Python):
a,b,c,k = (int(x) for x in input().split())
answer = 0
answer += min(a,k)
k = k - min(a,k)
k = k - min(b,k)
answer -= min(c,k)
print(answer)

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

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

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

a = int(a)
b = int(b)
c = int(c)
k = int(k)

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

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

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

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

Solution (Python):
from numba import njit
import numpy as np


def main(A, B, C, K):
    if K < A:
        return K
    if K > A+B:
        return A-(K-A-B)
    else:
        return A


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

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

result = 0

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

print(result)

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

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(A - (K - A - B))
    return


main()

Solution (Python):

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



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

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 k <= a:
    ans = k
else:
    count = k - a
    
    if count <= b:
        ans = a
    
    else:
        count -= b
        ans = a - count

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

Solution (Python):
a,b,c,k = map(int, open(0).read().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())
result = 0

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

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

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

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

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

if k > a :
  k = k-a
  if k > b :
    k = k-b
    if k < c :
      c = k
  else:
    c = 0
else:
  a = k
  c = 0

print(a-c)

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

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

Solution (Python):
import sys
INF = 10**9
sys.setrecursionlimit(INF)


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

    if k <= a:
        ans = k
    elif k <= a + b:
        ans = a
    else:
        
        ans = 2 * a + b - k

    print(ans)
    return(str(ans))


if __name__ == '__main__':
    main()

Solution (Python):
a, b, c, k = map(int, input().split())
s, f = 0, 0
while k > 0:
    s += min(a, k)
    k -= s
    if k > 0:
        f = min(b, k)
        k -= f
    if k > 0:
        s -= min(c, k)
        k -= s
    break
print(s)

Solution (Python):
import sys                                                          
a,b,c,k = [int(x) for x in sys.stdin.readline().rstrip().split(" ")]
s = 0                                                               
if k > 0:                                                           
    s += min(k, a)                                                  
    k -= a                                                          
if k > 0:                                                           
    k -= b                                                          
if k > 0:                                                           
    s -= k                                                          
print(s)

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

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

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

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

if In[3]-In[0] <= 0:
  print(In[3])
elif In[3] - (In[0]+In[1]) <= 0:
  print(In[0])
else:
  print(In[0]-(In[3]-In[0]-In[1]))

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

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

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

if K < A:
    res = K
else:
    res = A
    K -= A
    if K > B:
        K -= B
        if K > 0:
            res -= K

print(res)

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

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

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(' ')))
result = 0

if a > k:
    result += k
    k -= k
else:
    result += a
    k -= a

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

print(result)

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

nokori = K
num_1 = min(A, nokori)
nokori -= num_1
ans = num_1

nokori -= min(B, nokori)

num_minus = min(C, nokori)
ans -= num_minus

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):
from math import factorial as fact
import math
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
from collections import deque



def input1():
	return int(input())


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


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

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(range(n),r))



def all_sets(num_list):
	subsets=[]
	for i in range(2,len(num_list) + 1):
		for c in combinations(num_list, i):
			subsets.append(c) 
	return subsets


def ch(x1):
	cf=[]
	for i in range(2,math.sqrt(x1)+1):
		if x1 % i==0:
			cf.append(i)
	return cf

def CountOneRoots(x,y):
	
	return fact(x+y)/fact(x)/fact(y)


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
	
def count_str(m):
	tmp={}
	for i in range(10):
		if i in tmp:
			tmp[i]=1
		else:
			tmp[i]+=1
	return tmp

def is_prime(n):
	if n==1: return True

	for i in range(2,int(math.sqrt(n))+1):
		if n%i==0:
			return False
	return True

a,b,c,k=input2()

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

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

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

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

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

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