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 re
import math
from collections import defaultdict
import itertools
from copy import deepcopy
import random
from heapq import heappop,heappush
import time
import os
import queue
import sys
import datetime
from functools import lru_cache

readline=sys.stdin.readline
sys.setrecursionlimit(2000000)

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)


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

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):
A, B, C, K = map(int, input().split())
sum = 0
a = min(K, A)
K -= a
sum += a
b = min(K, B)
K -= b
sum += b * 0
c = min(K, C)
sum += c * (-1)
print(sum)

Solution (Python):
def ma():
    return map(int,input().split())
y="Yes"
t="No"
a,b,c,k=ma()
if a+b>=k:
    print(min(a,k))
else:
    print(a-(k-a-b))

Solution (Python):



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

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

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):
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, d=DVSR): return pow(x, d - 2, d)
def DIV(x, y, d=DVSR): return (x * INV(y, d)) % d
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

A,B,C,K = LI()

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

print(current)

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
num = min(a, k)
ans += num
k -= num

if k == 0:
    print(ans)

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

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

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

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

if 0 <= K <= A + B:
    print(min(A, K))
else:
    print(A - (K - (A + B)))

Solution (Python):
ip=[int(x) for x in input().split()]
a=ip[0]
b=ip[1]
c=ip[2]
k=ip[3]

ans=min(k,a)
k-=min(a,k)
if k>0:
	k-=min(k,b)
if k>0:
	ans-=min(k,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())
ans = 0
if A >= K:
    print(K)
else:
    ans += A
    K -= A
    if B < K:
        K -= B
        ans += -1 * K
    print(ans)

Solution (Python):
import bisect
import collections
import heapq
import math
import operator as op
from collections import Counter
from functools import reduce

import numpy as np


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def main():
    A, B, C, K = get_inputs(int)

    ans = 0
    if K > 0:
        N = min(K, A)
        ans += N
        K -= N

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

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

    assert K == 0
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

a,b,c,k = mi()

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

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

Solution (Python):
ma = lambda :map(int,input().split())
ni = lambda:int(input())
import collections
import itertools
import math
a,b,c,k = ma()
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 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(a-(k-a-b))

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

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

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

ans = 0
n = min(A, K)
ans += n
K -= n
if K == 0:
    print(ans)
    exit()
n = min(B, K)
K -= n
if K == 0:
    print(ans)
    exit()
n = min(C, K)
ans -= n
print(ans)

Solution (Python):
def main():
    a,b,c, k = map(int, input().split())
    if a >= k:
        return k
    if a + b >= k:
        return a
    return 2*a + b - k
print(main())

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

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


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):
def examA():
    S = SI()
    T = SI()
    if S==T[:len(S)]:
        if len(S)+1==len(T):
            print("Yes")
            return
    print("No")
    return

def examB():
    A, B, C, K = LI()
    ans = 0
    if A>=K:
        ans = K
    else:
        ans += A
        K -= A
        if K>B:
            K -= B
            ans -= K
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examB()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

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

x = min(a, k)
res += 1 * x
k -= x

x = min(b, k)
res += 0 * x
k -= x

x = min(c, k)
res += -1 * x
k -= x

print(res)

Solution (Python):
def main():

    import sys, math
    
    
    
    def data(): return sys.stdin.readline().strip()
    def mdata(): return list(map(int, data().split()))
    def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
    
    INF = int(1e9)
    mod = int(1e9)+7

    a,b,c,k=mdata()
    if k>a:
        if k>b:
            print(a-(k-a-b))
        else:
            print(a)
    else:
        print(k)

if __name__ == '__main__':
    main()

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

Solution (Python):
A, B, C, K = [int(i) for i in input().split()]
ans = 0
ans += min(A, K)
K = max(K - A, 0)
K = max(K - B, 0)
ans -= min(C, K)
print(ans)

Solution (Python):
import numpy as np

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

Solution (Python):
a, b, c, k = [int(i) for i in 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())
ans = 0
if a<k:
  ans += a
  k-=a
  if b <k:
    k-=b
    ans -= k
else:
  ans += k
print(ans)

Solution (Python):
a, b, c, k = map(int, input().split())
total = 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(a - (k - (a+b)))

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

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

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

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]





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

if k > 0:
    ans -= min(c, 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=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(a-(k-a-b))

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

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 = min(A,K) - max(0, K-A-B)
print(ans)

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


if __name__ == '__main__':
    resolve()

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

Solution (Python):
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    A,B,C,K = list(map(int,_in[0].split(' ')))  
    
    if A>=K:
        cnt = 1*K
    elif A+B>=K:
        cnt = 1*A
    else:
        cnt = 1*A-1*(K-A-B)
    
    print(cnt)

Solution (Python):
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():
    a, b, c, k = nm()

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


if __name__ == '__main__':
    main()

Solution (Python):



import math
import itertools
from collections import defaultdict
from collections import Counter
import numpy as np

import heapq

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

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

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

k-=b
print(a-k)

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

Solution (Python):
import sys

a, b, c, k = map(int, sys.stdin.readline().split())

def main():
    if k <= a + b:
        print(min(a, k))
    else:
        print(a - (k - (a + b)))
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(A-(K-A-B))

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

ans = min(a, k)

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

print(ans)

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)

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

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

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

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

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

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

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

Solution (Python):




geta = lambda fn: list(map(fn, input().split()))

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

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

Solution (Python):

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

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

if k > 0:
    k -= b
    
if k > 0:
    ans -= min(c, k)
    
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())

a=min(K,A)
b=min(K-a,B)
c=max(K-a-b,0)
ans=a*1+c*(-1)
print(ans)

Solution (Python):
import sys
from io import StringIO
import unittest


def resolve():
    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)))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2 1 1 3"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """1 2 3 4"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """2000000000 0 0 2000000000"""
        output = """2000000000"""
        self.assertIO(input, output)


if __name__ == "__main__":
    
    resolve()

Solution (Python):
import sys


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


sys.setrecursionlimit(20000000)


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


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

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    A, B, C, K = lr()
    ans = 0
    if K <= A:
        ans += K
        print(ans)
        return
    ans += A
    K -= A
    if K <= B:
        print(ans)
        return
    K -= B
    ans -= K
    print(ans)
resolve()

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6) 
import math
from copy import copy, deepcopy
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque 


from collections import Counter

from itertools import accumulate

from heapq import heapify,heappop,heappush




def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)

def main():
    mod = 10**9+7
    

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

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




if __name__ == "__main__":
    main()

Solution (Python):
import sys, os

def f():
    args = list(map(int,input().split()))
    if len(args) > 1:
        return args
    return args[0]

if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    a,b,c,k = f()
    ans = 0
    ac = min(a, k)
    k -= ac
    ans += ac

    bc = min(b, k)
    k -= bc

    cc = min(c, k)
    ans -= cc
    print(ans)
solve()

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

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

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

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

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

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

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

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

ak = min(a, k)
out1 = ak
k = k-ak
bk = min(b, k)
out2 = 0
k = k-bk
ck = min(c, k)
out3 = -ck

out = out1 + out2 + out3
print(out)

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


if __name__ == '__main__':
    solve()

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

Solution (Python):
import sys
input = lambda : sys.stdin.readline().strip()
a,b,c,k = map(int,input().split())
ans = 0
ans+=min(a,k)
k-=min(a,k)
k-=min(b,k)
ans-=min(c,k)
k-=min(c,k)
print(ans)

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

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):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


a, b, c, k = Is()
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)
else:
    k -= a

    if k <= b:
        print(a)

    else:
        k -= b

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

Solution (Python):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)
INF = 1 << 60


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


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


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

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

Solution (Python):
"""
A = 1
B = 0
C = -1
"""

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

if a >= k:
    print(k)
elif a+b >= k:
    print(a)
else:
    k -= a+b
    print(a-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):
from collections import defaultdict
a,b,c,k = map(int, input().split()) 




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

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):
def main():
    A, B, C, K = list(map(int, input().split(' ')))
    print(min([A, K]) - max([0, K - A - B]))


if __name__ == '__main__':
    main()

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

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

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

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

print(ans)

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


if __name__ == '__main__':
    main()

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

Solution (Python):

import os
import sys
from io import BytesIO, IOBase





def main():
    a,b,c,k=[int(x) for x in input().split()]
    ans=0
    
    m=min(a,k)
    ans+=m
    a-=m
    k-=m

    m=min(b,k)
    b-=m
    k-=m

    m=min(c,k)
    ans-=m
    c-=m
    k-=m

    print(ans)



BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



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