We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.
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-----
2 1 1 3

-----Sample Output-----
2

Consider picking up two cards with 1s 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.

Difficulty: introductory

Starter code:

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


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

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


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

print((k if a > k else a - max(k - a - b, 0)))


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

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

print(ans)


def __starting_point():
    S = input()
    a,b,c,k = map(int, S.split(sep=' '))

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

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

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


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

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

N = list(map(int,input().split()))
if(N[0] >= N[3]):print((N[3]))
else:
  if(N[1] >= (N[3]-N[0])):print((N[0]))
  else:print((N[0]-(N[3]-N[0]-N[1])))


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

ans = 0

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

k -= min(b, k)

ans -= min(c, k)

print(ans)


line = input().split(" ")
a=int(line[0])
b=int(line[1])
c=int(line[2])
k=int(line[3])
ans=0


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

a,b,c,k=map(int,input().split());ans=0
while k:
  if a>0 and k>0:
    if a<=k:
      ans+=a
      k-=a
      a=0
    elif a>k:
      ans+=k
      k=0
      a-=k
  if b>0 and k>0:
    if b<=k:
      k-=b
      b=0
    elif b>k:
      k=0
      b-=k
  if c>0 and k>0:
    if c<=k:
      ans-=c
      k-=c
      c=0
    elif c>k:
      ans-=k
      c-=k
      k=0
      
print(ans)

#ABC167B
a,b,c,k = map(int,input().split())
print(min(a,k)-max(k-a-b,0))

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

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

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


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)

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

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

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


import sys
def input(): return sys.stdin.readline().rstrip()

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

point = 0

if A <= K:
    point += A
    K -= A
else:
    point += K
    K = 0

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

point -= K

print(point)

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

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

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

    sum = 0

    n = k-a
    if n <= 0:
        sum = k
        print(sum)
        return
    else:
        sum = a

    n = n-b
    if n <= 0:
        print(sum)
        return
    
    n = n-c
    if n <= 0:
        sum = sum - (k-(a+b))
        print(sum)
        return

main()


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

    if k <= b :
        print(a)
    else :
        n = min(k,b)
        k -= b
        l = k
        print(1*m+(-1)*l)

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

[A, B, C, K] = list(map(int, list(input().split(' '))))
left = K
ans = 0
# A
left = K - A
if(left >= 0):
    ans += A
else:
    ans += K
# B .. do nothing as diff would be 0
left = left - B
# C .. no need to subtract the left anymore
if(left > 0):
    ans -= left

print(ans)



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

j1 = A - K
j2 = A + B - K
if j2 >0: # K < A + B
    if j1 < 0: #A > K
        print(A)
    else:
        print(K)
else:
    print(A + j2)

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

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


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

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

a, b, c, x = map(int, input().split())
ans = 0

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

x -= min(b, x)

ans -= min(c, x)
x -= min(c, x)
print(ans)

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

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

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

total = 0
if K <= A:
    print(K)
else:
    total += A
    K = K - A
    if K <= B:
        print(total)
    else:
        K = K - B
        if K <= C:
            total -= K
            print(total)
        else:
            total -= C
            print(total)

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


def __starting_point():
    main()

__starting_point()

a,b,c,k = input().split(" ")

if int(k) > int(a):
  num = int(a)*1
else:
  num = int(k)*1
  
if int(k) - (int(a) + int(b)) > 0:
  num += (int(k)-int(a)-int(b))*(-1)
else:
  num = num

print(num)


A,B,_,K=map(int,input().split());print(min(A,K,A*2+B-K))

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

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


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

import sys

if A >= K:
  print(K)
  return

r1 = K - A
cnt = A

if B >= r1:
  print(cnt)
  return

r2 = r1 - B
print((cnt - r2))


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

s = []
x = 0

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


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

def __starting_point():

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

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

    print(ans)


__starting_point()

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

ans = 0

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

print(ans)


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


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)

a,b,c,k = list(map(int,input().split()))
ans = min(a,k)
k = max(0,k-a-b)
ans -= k
print(ans)

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

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

print(ans)


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

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

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

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


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


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

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

if k > b:
    k -= b
elif k <= b:
    print(ans)
    return

print((ans-k))


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

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

cards=input()
new=cards.split()
a=int(new[0])
b=int(new[1])
c=int(new[2])
k=int(new[3])
if a>k:
    print(k)
else:
    if a+b>k:
        print(a)
    else:
        sum=a+(k-(a+b))*(-1)
        print(sum)


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


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

def answer(a: int, b: int, c: int, k: int) -> int:
    if k <= a:
        return k
    if k <= a + b:
        return a
    return a - (k - (a + b))


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


def __starting_point():
    main()
__starting_point()

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

num_A = min(A,K)
num_B = min(B,K-num_A)
num_C = min(C,K-num_A-num_B)

print(num_A-num_C)

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

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

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

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


a,b,c,d = map(int,input().split())
cnt = d-a
if cnt > 0:
    num = 1*a
    cnt -=b
    if cnt > 0:
        num = a + (d-a-b)*(-1) 
else:
    num = 1*d
print(num)

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


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

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

a,b,c,n = map(int,input().split())
ans = 0
if a >= n:
    ans = n
elif a+b >= n:
    ans = a
elif a+b < n:
    ans = -(n-a-b) + a

print(ans)

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

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

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

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

def ans167(A, B, C, K):
    if A>=K:
        return(K)
    elif A+B>=K:
        return(A)
    else:
        return(A-(K-A-B))

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

a,b,c,k = list(map(int,input().split()))
ans = 0
for x, y in zip([a,b,c], [1,0,-1]):
  if k <= 0:
    break
  ans += min(x,k) * y
  k -= min(x,k)

print(ans)

A,B,_,K=map(int,input().split());print(B-K+A*2if A+B<K else min(A,K))

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

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

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


def i_input(): return int(input())
def i_map(): return list(map(int, input().split()))
def i_list(): return list(map(int, input().split()))
def i_row(N): return [int(input()) for _ in range(N)]
def i_row_list(N): return [list(map(int, input().split())) for _ in range(N)]

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



a, b, c, k = list(map(int, input().split()))
ans = 0
if a >= k:
    ans = k * 1
else:
    ans = a * 1 + max(0, (k - a)) * 0 + max(0, (k - a - b)) * (-1)
print(ans)


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 + (-1) *(K - A - B))
else:
    print(A - B)

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

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

#!/usr/bin/env python3

#import
#import math
#import numpy as np
A, B, C, K = list(map(int, input().split()))

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




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

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

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


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

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

print(x)


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

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

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

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

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

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)

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

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

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

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)

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

A, B, C, K = map(int, input().split())
# 1:A枚, 0:B枚, -1:C枚, K枚を選ぶ
A = min(A, K)
B = min(K - A, B)
C = min(K - A - B, C)
assert A + B + C >= K
#print(A, B, C)
print(A - C)

import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp

INF = inf
MOD = 1000000007

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

tmp = 0
res = 0

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

print(res)


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


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


def easy_linear_programming():
    A, B, C, K = map(int, input().split())
    if K <= A:
        print(K)
    elif K - A <= B:
        print(A)
    else:
        k = K - A - B
        print(A - k)
        
easy_linear_programming()

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

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

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

max_val = 0
if k <= a:
    max_val += k
else:
    max_val += a
    k -= a
    if k <= b:
        pass
        # break
    else:
        k -= b
        max_val -= k

print(max_val)

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

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


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

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