CodeEff/ECCO huggingface dataset:
# Original solution
from functools import lru_cache
cc = [["." for i in range(8)] for j in range(8)]

@lru_cache(maxsize=None)     
def dia(s,a,b):
    d = []
    for i in range(8):
        for j in range(8):
            try:
                if j + (a - i) == b: 
                    d.append(s[i][j])
                if j -  (a - i) == b:
                    d.append(s[i][j])
                if j + (i - a) == b: 
                    d.append(s[i][j])
                if j -  (i - a) == b:
                    d.append(s[i][j])
            except IndexError: continue
    print(d)
    return d


def check(cc,a,b):
    ss = list(zip(*cc))
    if 'Q' in cc[a]: return False
    if 'Q' in ss[b]: return False
    d = []
    for i in range(8):
        for j in range(8):
            try:
                if j + (a - i) == b: 
                    d.append(cc[i][j])
                if j -  (a - i) == b:
                    d.append(cc[i][j])
                if j + (i - a) == b: 
                    d.append(cc[i][j])
                if j -  (i - a) == b:
                    d.append(cc[i][j])
            except IndexError: continue
    if 'Q' in d: return False
    return True

def chess(s,queen):
    if queen == 8:
        return s
    for i in range(8):
        for j in range(8):
            if check(s,i,j):
                sol = s
                sol[i][j] = 'Q'
                if chess(sol,queen+1) != [[]]:
                    return sol
                else: sol[i][j] = "."
    return [[]]


k = int(eval(input()))
for i in range(k):
    a, b = input().split()
    cc[int(a)][int(b)] = 'Q'
ans = chess(cc,k)
print(('\n'.join(list([''.join(x) for x in ans])) ))


# Optimized solution
from functools import lru_cache
cc = [["." for i in range(8)] for j in range(8)]

def check(cc,a,b):
    ss = list(zip(*cc))
    if 'Q' in cc[a]: return False
    if 'Q' in ss[b]: return False
    d = []
    for i in range(8):
        for j in range(8):
            try:
                if j + (a - i) == b: 
                    d.append(cc[i][j])
                if j -  (a - i) == b:
                    d.append(cc[i][j])
                if j + (i - a) == b: 
                    d.append(cc[i][j])
                if j -  (i - a) == b:
                    d.append(cc[i][j])
            except IndexError: continue
    if 'Q' in d: return False
    return True

def chess(s,queen):
    if queen == 8:
        return s
    for i in range(8):
        for j in range(8):
            if check(s,i,j):
                sol = s
                sol[i][j] = 'Q'
                if chess(sol,queen+1) != [[]]:
                    return sol
                else: sol[i][j] = "."
    return [[]]


k = int(eval(input()))
for i in range(k):
    a, b = input().split()
    cc[int(a)][int(b)] = 'Q'
ans = chess(cc,k)
print(('\n'.join(list([''.join(x) for x in ans])) ))
# Original solution
from functools import lru_cache
cc = [["." for i in range(8)] for j in range(8)]

def check(cc,a,b):
    ss = list(zip(*cc))
    if 'Q' in cc[a]: return False
    if 'Q' in ss[b]: return False
    d = []
    for i in range(8):
        for j in range(8):
            try:
                if j + (a - i) == b: 
                    d.append(cc[i][j])
                if j -  (a - i) == b:
                    d.append(cc[i][j])
                if j + (i - a) == b: 
                    d.append(cc[i][j])
                if j -  (i - a) == b:
                    d.append(cc[i][j])
            except IndexError: continue
    if 'Q' in d: return False
    return True

def chess(s,queen):
    if queen == 8:
        return s
    for i in range(8):
        for j in range(8):
            if check(s,i,j):
                sol = s
                sol[i][j] = 'Q'
                if chess(sol,queen+1) != [[]]:
                    return sol
                else: sol[i][j] = "."
    return [[]]


k = int(eval(input()))
for i in range(k):
    a, b = input().split()
    cc[int(a)][int(b)] = 'Q'
ans = chess(cc,k)
print(('\n'.join(list([''.join(x) for x in ans])) ))


# Optimized solution
from functools import lru_cache
cc = [["." for i in range(8)] for j in range(8)]
 
def check(cc,a,b):
    ss = list(zip(*cc))
    if 'Q' in cc[a]: return False
    if 'Q' in ss[b]: return False
    d = []
    for i in range(8):
        for j in range(8):
            try:
                if j + abs(a - i) == b: 
                    d.append(cc[i][j])
                if j -  abs(a - i) == b:
                    d.append(cc[i][j])
            except IndexError: continue
    if 'Q' in d: return False
    return True
 
def chess(s, queen):
    if queen == 8:
        return s
    for i in range(8):
        for j in range(8):
            if check(s, i, j):
                sol = s
                sol[i][j] = 'Q'
                if chess(sol, queen+1) != [[]]:
                    return sol
                else: sol[i][j] = "."
    return [[]]
 
 
k = int(eval(input()))
for i in range(k):
    a, b = [int(x) for x in input().split()]
    cc[a][b] = 'Q'
ans = chess(cc, k)
print(('\n'.join(list([''.join(x) for x in ans])) ))
# Original solution
L = list(map(int, input().split()))
maxres = 0

for i in range(3):
    n = [1, 0, -1]
    if L[i] <= L[3]:
        maxres += L[i] * n[i]
        L[3] -= L[i]
    else:
        for j in range(L[3]):
            maxres += n[i]
            L[3] -= 1
print(maxres)

# Optimized solution
L = list(map(int, input().split()))
n = [1, 0, -1]
maxres = 0

for i in range(3):
    if L[3] == 0:
        break
    maxres += L[i] * n[i]
    L[3] -= L[i]
    #print(L[3])
    if L[3] < 0:
        maxres -= abs(L[3]) * n[i]
        break
print(maxres)
# Original solution
A, B, C, K = list(map(int, input().split()))
point = 0
judge = False
if A == K:
  point = A
  judge = True
elif A == 0 and B == K:
  point = 0
  judge = True
elif A == 0 and B == 0 and C == K:
  point = -K
  judge = True
if not judge:
  if A > 0 and K > 0:
    while A > 0 and K > 0:
      K -= 1
      A -= 1
      point += 1
  if B > 0 and K > 0:
    while B > 0 and K > 0:
      K -= 1
      B -= 1
      point += 0
  if C > 0 and K > 0:
    while C > 0 and K > 0:
      K -= 1
      C -= 1
      point -= 1
print(point)

  

# Optimized solution
a,b,c,k=list(map(int,input().split()));count=0
if k<=a: print(k)
elif k<=a+b: print(a)
else: print((a-(k-a-b)))
# Original solution
a,b,c,k = list(map(int,input().split()))

res = 0

if a > 0:
    if a <= k:
        res += a
        k -= a
    elif a > k:
        while k > 0:
            res += 1
            k -= 1

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

if k > 0 and c > 0:
    if c <= k:
        res -= c
        k -= c
    elif c > k:
        while k > 0:
            res -= 1
            k -= 1

print(res)

# Optimized solution
a,b,c,k = list(map(int,input().split()))
res = 0

if a > 0:
    if a <= k:
        res += a
        k -= a
    elif a > k:
        res += k
        k = 0

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

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

print(res)
# Original solution
from collections import *

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


# Optimized solution
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)))
# Original solution
a, b, c, k = list(map(int, input().split()))

res = 0
i = 0

a_times = 0
b_times = 0
c_times = 0

if a == k:
    a_times = k
else:
    for i in range(k):
        # print('i = ', i)
        if i < a:
            a_times += 1
            # print('res + 1= ', res)
        elif i + a < a + b:
            b_times += 1
            # print('res + 0 = ', res)
        elif i + a + b < a + b + c:
            c_times += 1
            # print('res - 1 = ', res)

print((a_times * 1 + b_times * 0 + c_times * -1))


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

a_times = 0
b_times = 0
c_times = 0

if a <= k:
    a_times = a
    # print('a_times = ', a_times)
    if a_times + b <= k:
        b_times = b
        # print('if - b_times = ', b_times)
    else:
        b_times = k - a_times
        # print('else - b_times = ', b_times)
    if a_times + b_times + c <= k:
        c_times = c
        # print('if - c_times = ', c_times)
    else:
        c_times = k - a_times - b_times
        # print('else - c_times = ', c_times)
else:
    a_times = k


print((a_times * 1 + b_times * 0 + c_times * -1))
# Original solution

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

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

# Optimized solution
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))))
# Original solution
def main():

    A, B, C, K = list(map(int, input().split()))
    v = 0
    if K <= A:
        return K
    elif K <= A+B:
        return A
    else:
        return A - (K-A-B)

if __name__ == '__main__':
    print((main()))


# Optimized solution
def main():

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


if __name__ == '__main__':
    print((main()))
# Original solution
A, B, C, K = list(map(int, input().split()))

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

print((a-c))

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

ans = 0

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

ans += min(b,k)*0
k -= min(b,k)

ans += k*(-1)

print(ans)
# Original solution
a,b,c,k=list(map(int,input().split()))
if a>=k:
  print(k)
elif a<k:
  if k-a<=b:
    print(a)
  elif k-a >b:
    print((a-(k-a-b)))


# Optimized solution
a,b,c,k=list(map(int,input().split()))
ans=0
ans=min(k,a)
k-=min(k,a)
ans+=min(k,b)*0
k-=min(k,b)
ans+=min(k,c)*(-1)
print(ans)
# Original solution
a,b,c,k=list(map(int,input().split()))
if k<=a+b:
    print(a)
for i in range(1,c):
    if k==a+b+i:
        print((a-i))

# Optimized solution
a,b,c,k=list(map(int,input().split()))
 
def calc(a,b,c,k):
    if k<a:
        return k
    elif k<=a+b:
      return a
    else:
        return a-(k-a-b)
print((calc(a,b,c,k)))
# Original solution
A,B,C,K = list(map(int,input().split()))
if A >= K:
    print(K)
else :
    if (K-A) <= B:
        print(A)
    else:
        print((A-(K-A-B)))

# Optimized solution
A,B,C,K = list(map(int ,input().split()))
if A>=K:
    print(K)
else:
    if A+B>=K:
        print(A)
    else:
        print((A-(K-A-B)))
# Original solution
a, b, c, k = list(map(int, input().split()))

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

# Optimized solution
a, b, c, k = list(map(int, input().split()))
if k <= a+b:
    print((min(k, a)))
else:
    print((min(a, (a - (k - (b+a))))))
# Original solution
A, B, C, K = list(map(int, input().split()))

count = 0
second = K-A-B

if A >= K:
  count = K
else:
  count = A

if second:
  for j in range(second):
    count -= 1

print(count)
  

# Optimized solution
A, B, C, K = list(map(int, input().split()))

count = 0
second = K-A-B

if A >= K:
  count = K
else:
  count = A

if second > 0:
    count -= second

print(count)
# Original solution
A, B, C, K = list(map(int, input().split()))
Ans = 0

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


# Optimized solution
A, B, C, K = list(map(int, input().split()))

if A >= K:
    print(K)
elif A + B >= K:
    print(A)
else:
    print((2*A + B - K))
# Original solution
a,b,c,k = list(map(int, input().split()))
count = 0

if a <= k:
    count += a
else:
    count += k
    print(count)
    exit()
k -= a

if b <= k:
    k -= b
else:
    print(count)
    exit()


count -= k

print(count)

# Optimized solution
A,B,C,K=list(map(int,input().split()))
if (A+B)>=K and A<=K:
    print(A)
elif (A+B)>=K and A>=K:
    print(K)
elif (A+B)<K:
    print((A-(K-A-B)))
# Original solution
#S = str(input())
A, B, C, K = list(map(int,input().split()))

if A <= K:
    a = A
else:
    a = K

if B <= K-a:
    b = B
else:
    b = K-a

c = K - a - b

ans = a - c
print(ans)

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


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

print((xa - xc))
# Original solution
# B - Easy Linear Programming

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


# Optimized solution
# B - Easy Linear Programming

a, b, c, k = list(map(int, input().split()))
print((min(k, a, a + a + b - k)))
# Original solution
a, b, c, k = list(map(int, input().split()))
box = []
result = 0

if c == 0:
    if a >= k:
        print(k)
    else:
        print(a)
    quit()

for i in range(a):
    box.append(1)
for i in range(b):
    box.append(0)
for i in range(c):
    box.append(-1)

for i in range(k):
    result = result + box[i]

print(result)

# Optimized solution
a, b, c, k = list(map(int, input().split()))
box = []
result = 0

if k <= a:
    print(k)
    quit()
else:
    k = k - a
    result = a
    if k <= b:
        print(result)
        quit()
    else:
        k = k - b
        if k <= c:
            result = result - k
            print(result)
            quit()
        else:
            result = result -c
            print(result)
            quit()
# Original solution
A,B,C,K=list(map(int,input().split()))
if A+B>=K:
    if A<=K:
        print(A)
    else:
        print(K)
else:
    print((A*2-K+B))


# Optimized solution
A,B,C,K=list(map(int,input().split()))

if K<=A+B:
    print((min(A,K)))
else:
    print((A-(K-A-B)))
# Original solution
#枚数
a, b, c, k = list(map(int, input().split()))
 
A = a
B = 0
C = -1 * c
 
if k - a >= 0:
    if k -a -b > 0:
        for i in range(c +1):
            if a + b + i == k:
                print((a + (-1)*i))
    else:
        print(a)
else:
    print(k)
 

# Optimized solution
#枚数
a, b, c, k = list(map(int, input().split()))
 
A = a
B = 0
C = -1 * c
 
if k - a >= 0:
    if k -a -b >= 0:
        print((a + (-1)*(k -a -b) ))
    else:
        print(a)
else:
    print(k)
# Original solution
A,B,C,K = list(map(int,input().split()))
result = 0
#A分引く
if (K - A) < 0:
    for i in range(A):
        if K <= 0:
            break
        result = result + 1
        K  -= 1
else:
    K -= A
    result += 1*A

if (K-B) < 0:
    for i in range(B):
        if K <= 0:
            break
        K -= 1
else:
    K -= B

if (K-C) < 0:
    for i in range(C):
        if K <= 0:
            break
        K -= 1
        result -= 1
else:
    K -= C
    result -= C
print(result)
    


# Optimized solution
A,B,C,K = list(map(int,input().split()))

def abc(A,B,C,K):
    result = 0
    #A分引く
    K = K - A
    result = A + result
    if K < 0:
        K = K + A
        result -= abs(abs(K)-A)
        return result
    K -= B
    if K < 0:
        return result
    K -= C
    result -= C
    if K < 0:
        K += C
        result += abs(abs(K)-C)
    return result
print((abc(A,B,C,K)))
# Original solution
A, B, C, K = list(map(int, input().split()))
if K >= A+B:
    ans = A - (K-A-B)
else:
    ans = K
print(ans)


# Optimized solution
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)))
# Original solution
import math
from functools import reduce
import itertools
A,B,C,K = list(map(int,input().split()))

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

    else:
        for i in range(K-(A+B)):
            count-=(K-(A+B))
            ans=count

print(ans)


# Optimized solution
import math
from functools import reduce
import itertools
A,B,C,K = list(map(int,input().split()))

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

    else:
        count-=(K-(A+B))
        ans=count

print(ans)
# Original solution
def main():
    A,B,_,K=list(map(int,input().split()))
    print((min(A, K) - max(0, K-A-B)))
    return

if __name__=='__main__':
    main()

# Optimized solution
def main():
    A,B,C,K=list(map(int,input().split()))
    print((min(A, K) - max(0, K-A-B)))
    return

if __name__=='__main__':
    main()
# Original solution
A,B,C,K = list(map(int, input().split()))
count = 0

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

elif A > K:
    print(K)
elif A+B > K:
    print(A)
else:
    for i in reversed(list(range(0, K))):
        if A > 0:
            count += 1
            A = A - 1
        elif B > 0:
            B = B - 1
        else: 
            count -= 1

    print(count)

# Optimized solution
A,B,C,K = list(map(int, input().split()))
count = 0

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

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

    print((A - t))
# Original solution
A, B, C, K = list(map(int, input().split()))
result = 0

if A >= K:
    print(K)
else:
    for i in range(K):
        if A > (i + 1):
            result += 1
            continue
        elif A == (i + 1):
            result += 1
            continue

        if B > (i + 1 - A):
            result += 0
            continue
        elif B == (i + 1 - A):
            result += 0
            continue

        if C > (i + 1 - A - B):
            result -= 1
            continue
        elif C == (i + 1 - A - B):
            result -= 1
            break
        else:
            break
    print(result)

# Optimized solution
A, B, C, K = list(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)))
elif (A + B + C) == K:
    print((A - C))
# Original solution
A, B, C, K = list(map(int, input().split()))

if A > K:
    a = K
else:
    a = A
    
if (A + B) < K:
    for i in range(0,K-A-B):
        a = a - 1
print(a)

# Optimized solution
A, B, C, K = list(map(int, input().split()))

if A > K:
    a = K
else:
    a = A
    
if (A + B) < K:
    a = a - K + A + B
print(a)
# Original solution
a, b, c, k = input().split()
a, b, c, k = int(a), int(b), int(c), int(k)
result = 0

if a == k:
    result = a
else:
    for i in range(k):
        if a != 0:
            result = result + 1
            a = a - 1
        elif a == 0 and b != 0:
            #result変わらず
            b = b - 1
        elif a == 0 and b == 0 and c != 0:
            result = result - 1
            c = c - 1
        else:
            break

print(result)


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

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

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

print((a-(k-a-b)))
# Original solution
# -*- coding: utf-8 -*-
# 標準入力の取得
A, B, C, K = list(map(int, input().split()))

# 求解処理
result = int()
if K <= A:
    result = K
elif A < K and K <= A + B:
    result = A
elif A + B < K and K <= A + B + C:
    result = 2*A + B - K

# 結果出力
print(result)


# Optimized solution
# -*- coding: utf-8 -*-


def get_input() -> tuple:
    """標準入力の取得

    Returns:
        tuple -- 標準入力
    """
    A, B, C, K = list(map(int, input().split()))
    return A, B, C, K


def main(A: int, B: int, C: int, K: int) -> None:
    """求解処理.
    Kの値で場合分けする.

    Arguments:
        A {int} -- 整数
        B {int} -- 整数
        C {int} -- 整数
        K {int} -- 整数
    """
    result = int()
    if K <= A:
        result = K
    elif A < K and K <= A + B:
        result = A
    elif A + B < K and K <= A + B + C:
        result = 2*A + B - K

    # 結果出力
    print(result)


if __name__ == "__main__":
    A, B, C, K = get_input()
    main(A, B, C, K)
# Original solution

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


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

ans = 0
if a >= k:
    print(k)
elif a+b >= k:
    print(a)
else:
    ans += a
    k -= (a+b)
    ans -= k
    print(ans)
# Original solution
a,b,c,k = list(map(int,input().split()))

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


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

if a >= k:
    print(k)
elif a+b >= k:
    print(a)
else:
    k -= (a+b)
    print((a-k))
# Original solution
A,B,C,K = list(map(int,input().split()))
if A >= K:
    print(K)
elif (A+B)>=K:
    print(A)
else:
    print((A*2+B-K))


# Optimized solution
A,B,C,K = list(map(int,input().split()))
if K <= A:
    print(K)
elif K <= A+B:
    print(A)
else:
    print((2*A+B-K))
# Original solution
a, b, c, k = list(map(int, input().split(' ')))
if a+b>k:
    print(k)
else:
    if a+b+c<=k:
        print((a-c))
    else:
        print((a-(k-(a+b))))

# Optimized solution
a, b, c, k = list(map(int, input().split(' ')))
if a+b>k:
    print(k)
elif a+b+c<=k:
    print((a-c))
else:
    print((a-(k-(a+b))))
# Original solution
a, b, c, k=input().split()
a=int(a)
b=int(b)
c=int(c)
k=int(k)
ans=0

if a>0 and k>0 and a<=k:
  k=k-a
  ans=ans+a
elif a>0 and k>0 and a>k:
  ans=ans+k
  k=0

while b>0 and k>0:
  b+=-1
  k+=-1

while c>0 and k>0:
  ans+=-1
  c+=-1
  k+=-1
  
print(ans)

# Optimized solution
a, b, c, k=input().split()
a=int(a)
b=int(b)
c=int(c)
k=int(k)
ans=0

if a>0 and k>0 and a<=k:
  k=k-a
  ans=ans+a
elif a>0 and k>0 and a>k:
  ans=ans+k
  k=0

if b>0 and k>0 and b<=k:
  k=k-b
elif b>0 and k>0 and b>k:
  k=0
  
if c>0 and k>0 and c<=k:
  k=k-c
  ans=ans-c
elif c>0 and c>0 and c>k:
  ans=ans-k
  k=0

  
print(ans)
# Original solution
A,B,C,K=(int(x) for x in input().split())

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

# Optimized solution
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)
# Original solution
# coding: utf-8
a,b,c,k = list(map(int, input().split()))
sum = 0
if b == 0 and c == 0:
    print(k)
else:
    for i in range(k):
        if a > 0:
           sum = sum + 1
           a = a - 1
        elif b > 0:
            b = b - 1
        elif c > 0:
            sum = sum - 1
            c = c - 1
    print(sum)

# Optimized solution
# coding: utf-8
import sys
# sys.exit()
a,b,c,k = list(map(int, input().split()))
# sum = 0
if k <= a:
    sum = k
    print(sum)
    sys.exit()
else:
    sum = a
    k = k - a
if k <= b:
    print(sum)
    sys.exit()
else:
    k = k - b
    sum = sum - k
    print(sum)
# Original solution
A, B, C, K = list(map(int, input().split()))

sum=0

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

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

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

print(sum)

# Optimized solution
A, B, C, K = list(map(int, input().split()))

sum = 0

X_a = min(A,K)
sum += X_a
K -= X_a

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

X_c = min(C,K)
sum -= X_c

print(sum)
# Original solution
a,b,c,k = list(map(int, input().split()))
print((min(k,a) - (k-a-b if k-b>a else 0)))


# Optimized solution
a,b,c,k = list(map(int, input().split()))
print((min(k, a) - max(0, k-a-b)))
# Original solution
a, b, c, k = list(map(int, input().split()))

cnt = 0

if k >= a:
    k = k - a #3
    cnt = cnt + 1*a  #1

    if k >= b:
        k = k - b #1

        if k >= c:
            cnt = cnt + -1*c
            k = k - c

        else:
            for i in range(0, k):
                cnt += -1
                k -= 1
    
    else:
        for i in range(0, k):
            k -= 1

else:
    for i in range(0, k):
        cnt += 1
        k -= 1
print(cnt)

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

cnt = 0

if k >= a:
    k = k - a #3
    cnt = cnt + 1*a  #1

    if k >= b:
        k = k - b #1

        if k >= c:
            cnt = cnt + -1*c
            k = k - c

        else:
            cnt = cnt + -1*k
            k = k - k
    
    else:
        k = k - k

else:
    cnt = cnt + 1*k
    k = k - k
    
print(cnt)