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

2 1 1 3

Sample Output 1

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.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000



Sample input:
2 1 1 3


Sample output:
2

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if k >= a:
    s = a
else:
    s = k
if (k - a - b) >= 0:
    s -= k - a - b

print(s)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
a,b,c,k = map(int,input().split())
 
an = 0
if k<=a:
    an = k
    print(an)
    sys.exit()
else:
    an = a
    k -= a
if k<=b:
    print(an)
    sys.exit()
else:
    k -= b
an -= k
print(an)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

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

g = 0
gc = d

if d - a > 0 and a > 0:
    g += a
    d -= a
elif a > 0:
    g += d
    print(str(g))
    sys.exit()

if d - b > 0 and b > 0:
    d -= b
elif b > 0:
    print(str(g))
    sys.exit()

if d == c and c > 0:
    g -= d
elif c > 0:
    g -= d

print(str(g))

Submission status: Accepted

Solution code:
#枚数
a, b, c, k = 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)

Submission status: Accepted

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

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

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
A,B,C,K = (int(x) for x in input().split())

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

    else:
        print(ans)
else:
    print(K)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

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

kk=k-a
for i in range(1):
    if kk<0:
        ans=k
        k=-1
        break
    else:
        ans = a
        k=k-a
    kk = k-b
    if kk<0:
        k=-1
        break
    else:
        k=k-b

    kk = k-c

    if kk<0:
        ans=ans-k
    elif kk>=0:
        ans=ans-c
print(ans)

Submission status: Accepted

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

if a >= k:
  ak = k
  print(ak)
  exit()
else:
  ak = a
  
ck = (k-a-b)*-1

print(ak+ck)

Submission status: Accepted

Solution code:
N = input()
n =[int(x) for x in N.split()]
A = n[0]
B = n[1]
C = n[2]
K = n[3]
if A>=K:
    print(K)
elif K <= A+B:
    print(A)
else:
    print(2*A+B-K)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
line1 = input().split()
a = int(line1[0])
b = int(line1[1])
c = int(line1[2])
k = int(line1[3])
if a >= k:
  print(k)
elif a + b >= k:
  print(a)
else:
  print(a - (k-(a + b)))

Submission status: Accepted

Solution code:
A,B,C,K=input().split(' ')
A=int(A)
B=int(B)
C=int(C)
K=int(K)
S=0
if (A>=K):
    S=K
elif (A+B>=K):
    S=A
else:
    S=A-(K-A-B)
print(S)

Submission status: Accepted

Solution code:
A, B, C, K = map(int, input().split())
D = 0
if K <= A:
    print(K)
else:
    K -= A
    D += A
    if K <= B:
        print(D)
    else:
        K -= B
        D += -1 * K
        print(D)

Submission status: Accepted

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

if a >= d:
    print(d*1)
elif a < d:
    if d-(a+b) <= 0:
        print(a)
    elif d-(a+b+c) <= 0:
        print(a + (d-a-b)*(-1))

Submission status: Accepted

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

if K <= A:
    print(K)
elif K <= A + B:
    print(A)
elif K <= A + B + C:
    ans = A - (K - A - B)
    print(str(ans))
else:
    print(0)



# # https://atcoder.jp/contests/abs/tasks/abc081_b

# N = int(input())
# A = map(int, input().split())

# min_count = 1000000000000

# for a in A:
#     pow_count = 0

#     while a % 2 == 0:
#         pow_count += 1
#         a /= 2
#     # print(a, pow_count)

#     if pow_count < min_count:
#         min_count = pow_count

# print(min_count)

Submission status: Accepted

Solution code:
s = input().split()
n = int(s[3])
sum_=0
if int(s[0]) >= n:
    sum_=1*n
elif int(s[0])+int(s[1]) >= n:
    sum_=1*(n-int(s[1]))
elif int(s[0])+int(s[1])+int(s[2]) >= n:
    sum_=1*int(s[0])+(-1*(n-int(s[0])-int(s[1])))

print(sum_)

Submission status: Accepted

Solution code:
def xyz(a,b,c,k):
    if k==a:
        return(k)
    if a>k:
        return(k)
    sum1=a
    if a+b>=k:
        return(a)
    d=k-a-b
    sum1=sum1-d
    return(sum1)
    
    


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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A,B,C,K= map(int, input().split())
if A >= K:
    print(K)
    #print("a")
else:
    ans = A
    if B >= K-A:
        print(A)
        #print("b")
    else:
        print(A-(K-(A+B)))
        #print("c")

Submission status: Accepted

Solution code:
a,b,c,k = map(int,input().split())
l = list()
if a <= k:
  l.append(1*a)
  k = k-a
  if b <= k:
    k = k-b
    if c <= k:
      l.append(-1*k)
    else:
      l.append(-1*k)
  else:
    l.append(0)
else:
  l.append(k)
print(sum(l))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A,B,C,K=map(int,input().strip().split())
if K-A>0:
  K=K-A
  Sum=A
  K=K-B
  if K>0:
    if K>C:
      print(Sum-C)
    else:
      print(Sum-K)
    #if K>C:
  else:
    print(Sum)
else:
	print(K)

Submission status: Accepted

Solution code:
lis = list(map(int, input().split()))
k,mmm,all_=lis[3],[1,0,-1],0
sum_=[0]*3
for i in range(3):
  #print('%d %d'%(k, lis[i]))
  if lis[i]<k:
    sum_[i]+=lis[i]
    k-=lis[i]
  elif lis[i]>=k:
    sum_[i]+=k
    k=0

#print(sum_)
for j in range(3):
  all_+=mmm[j]*sum_[j]
print(all_)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A,B,C,K = list(map(int,input().split(" ")))
r=0
if A>0:
  r+=min(K,A)
  K-=A
if B>0 and K>0:
  K-=B
if C>0 and K>0:
  r-=K
print(r)

Submission status: Accepted

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

ans=0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

sums = 0

if (k <= a):
    print(k)
    sys.exit()
else:
    sums += a
    k -= a

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

sums += -1 * k
print(sums)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(a, b, c, k):
    point = min(a, k)
    more = k - a
    if more > 0:
        more -= b
    if more > 0:
        point -= more

    print(point)

a, b, c, k = map(int, open(0).read().split())
solve(a, b, c, k)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
a, b, c, k  = map(int, input().split())
if k <= a:
    print(k)
elif k > a and k <= a+b :
    print(a)
elif k > a+b and k <= a+b+c:
    print(a - (k - (a+b)))

Submission status: Accepted

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

ans = a
cnt = k-a
if cnt <=0:
    print(min(ans,k))
    exit()

cnt = cnt -b
if cnt <= 0:
    print(ans)
    exit()

ans = ans-cnt

print(ans)

Submission status: Accepted

Solution code:
A,B,C,K=map(int,input().split())
d=A-K
e=B-abs(d)
if d>0:
    print(K)
if d==0:
    print(A)
if d<0:
    if e>0:
        print(A+abs(d)*0)
    if e==0:
        print(A+B*0)
    if e<0:
        print(A+B*0+abs(e)*(-1))

Submission status: Accepted

Solution code:
a,b,c,k=map(int,input().split())
if a>=k:
  print(k)
  exit()
else:
  k-=a
  if b>=k:
    print(a)
    exit()
  else:
    k-=b
    if c>=k:
      print(a-k)
    else:
      print(a-c)

Submission status: Accepted

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

a1 = a
nokori = 0
nokori_1 = 0
nokori_2 = 0
tensuu = 0

if k-a1 > 0:
   tensuu +=a1
   nokori = k-a

if nokori > 0:
   tensuu +=0
   nokori2 =nokori - b
   if nokori2 > 0:
        tensuu -= nokori2
if k - a1 <= 0:
    tensuu = k
print(tensuu)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def Main(A, B, C, K):
    if K < A:
        return K
    elif A <= K <= A+B:
        return A
    else:
        return 2*A+B-K
        
def main():
    A, B, C, K = map(int, input().split())
    print(Main(A, B, C, K))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a, b, c, k = map(int, input().split())
# print(a)
# print(b)
# print(c)
# print(k)

# sum = (1) * (a) + (0) * (k-a) + (-1) * (k-a-b)

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

# if k-a <= b:
#   sum = sum + (0) * (k-a)
# else:
#   sum = sum + (0) * b

# sum = sum + (-1) * (k-a-b)

# print('sum')
print(sum)

Submission status: Accepted

Solution code:
body_1,body_2,body_3,cards=map(int,input().split())
total=0
sa=cards-body_1-body_2
if body_1>=cards:
    total=cards
elif body_1<cards:
    if (body_1+body_2)>=cards:
        total=body_1*1
    else:
        total=body_1-sa

print(total)

Submission status: Accepted

Solution code:
def check(num, k):
    if(num == k):
        return True
    else:
        return False


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

    if(k<=a):
        if(k==a):
            ans = a
        else:
            ans = k
    else:
        ans = a
        chosen = a
        if((k-chosen)>b):
            chosen += b
            if((k-chosen)<=c):
                if((k-chosen)==c):
                    ans += (-1*c)
                else:
                    ans += (-1*(k-chosen))

    print(ans)
 
main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin
A, B, C, K = [int(x) for x in stdin.readline().rstrip().split()]
if K-A <= 0:
    print(K)
elif K-A > 0:
    if K-A-B <= 0:
        print(A)
    else:
        print(int(A+(K-A-B)*-1))

Submission status: Accepted

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

def solve(a,b,c,k):
  if a >= k:
    return k
  elif a + b >= k:
    return a
  else:
    return a-(k-a-b)
    
print(solve(A,B,C,K))

Submission status: Accepted

Solution code:
X = list(map(int,input().split()))
A = X[0]
B = X[1]
C = X[2]
K = X[3]
score=[1,0,-1]
max_score = 0
nums = []

if (K>=A):
  max_score += 1*A
if (K>=A+B):
  max_score += 0*B
if (K-A-B>0):
  max_score += (-1)*(K-A-B)

if(A>K):
  max_score += 1*K
  
print(max_score)

Submission status: Accepted

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

Submission status: Accepted

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


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

Submission status: Accepted

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

ans = 0
ans = min(A, K)
K -= ans
K -= min(B,K)
ans -= min(C, K)

print(ans)

Submission status: Accepted

Solution code:
A, B, C, K = 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)

Submission status: Accepted

Solution code:
a,b,c,k = map(int,input().split())
s = k
d = k - a
k -= a
e = k - b
k -= b
f = k - c
n = 0 
if d == 0:
  print(a)
elif s < a:
  print(s)
elif e == 0 or e < 0:
  print(a)
elif f == 0 :
  print(a - c)
else:
  print(a - e)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
n = list(map(int,input().split()))

sum = 0
num = 0
nm = [1,0,-1]
c = n[3]
cu =0


for i in n:
    if c == 0:
        break
    if i > c:
        sum+= nm[num]*c
        break
    if i <= c:
        sum += nm[num] * i
        c-=i
        num+=1




print(sum)

Submission status: Accepted

Solution code:
a,b,c,d = map(int,input().split())
 
ans = 0
 
if a >= d:
    ans = d
elif (a + b) >= d:
    ans = a
else:
    ans = a - (d - (a+b))
    
    
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

if A > K:
    ans += K * 1
else:
    ans += A * 1

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
s=list(map(int,input().split()))
if s[3]<=s[0]:
  print(s[3])
else:
  if s[3]-s[0]<=s[1]:
    print(s[0])
  else:
    if s[3]-s[0]-s[1]<=s[2]:
      print(2*s[0]+s[1]-s[3])
    else:
      print(s[0]-s[2])

Submission status: Accepted

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

if A >= K:
    s = s + K
    print(s)

else:
    s = A
    #print("s2",s)
    K = K - A
    #print("K",K)

    if B >= K:
        s = s
        print(s)

    else:
        s = s
        #print("s4",s)
        K = K - B
        #print("K",K)

        if C >= K:
            s = s - K
            print(s)
        else:
            s = s - C
            K = K - C
            print(s)

Submission status: Accepted

Solution code:
def main():
	inputList= list(map(int, input().split()))
	score =[1,0,-1]
	ans =0
	for i in range(len(inputList)-1):
		if inputList[3] >= inputList[i]:
			inputList[3] = inputList[3] - inputList[i]
			ans = ans + inputList[i]*score[i]
		else:
			ans = ans + inputList[3]*score[i]
			return ans
	return ans
     
if __name__ == '__main__':
	print(main())

Submission status: Accepted

Solution code:
a,b,c,k=map(int,input().split())
ct=k
result = 0
if a>0:
  if ct>a:
    result=1*a
  elif a>=ct:
    result=1*ct
  ct=k-a
if ct>0 and b>0:
  ct=ct-b
if ct>0 and c>0:
  result=result+(-1*ct)
print(result)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
arr = input().split(" ")

a = int(arr[0])
b = int(arr[1])
c = int(arr[2])
d = int(arr[3])

e=a+b

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

##### dict の for文 #####
# for k, v in d.items():
#     print(k, v)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	A,B,C,K = inputMap()

	ans = 0
	for i in range(K):
		if A > 0:
			if A >= K:
				print(K)
				sys.exit()
			else:
				K -= A
				ans += A
				A = 0
				#print(K)
		elif B > 0:
			if B >= K:
				print(ans)
				sys.exit()
			else:
				K -= B
				B = 0
		else:
			print(ans-K)
			sys.exit()

	print(ans)



#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# N 個のボールを K グループに分ける場合のパターン数
def sunuke(N, K, mod=10**9+7):
	if N < K or K-1 < 0:
		return 0
	else:
		return combination(N-1, K-1, mod)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
num = input().split()
A = int(num[0]) #1
B = int(num[1])  #0
C = int(num[2]) #-1
K = int(num[3]) #選ぶ個数

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

Submission status: Accepted

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

Submission status: Accepted

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

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


if b>=k:
    print(ans)
    exit()
else:
    k=k-b


print(ans-k)

Submission status: Accepted

Solution code:
a, b, c, k = map(int, input().split())
if a > k:
    a_num = k
else:
    a_num = a
b_num = k - a_num
if b_num > b:
    b_num = b
else:
    b_num = b_num
c_num = k - (a+b)
if c_num > c:
    c_num = c
else:
    c_num = c_num

if k < a:
    ans = a_num * 1
elif k < (a+b):
    ans = a_num * 1 + b_num * 0
else:
    ans = a_num * 1 - c_num * 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# 1 # 0 # -1 K
a, b, c, K = map(int, input().split())
sum = 0

if K >= a:
  K = K - a
  sum += a*1
elif K != 0:
  sum += int(K)*1
  K = 0
  
if K >b:
  sum +=b*0
  K = K - b
elif K != 0:
  sum += int(K)*0
  K = 0
  
if K > c:
  sum +=c*-1
  K = K -c
elif K!=0:
  sum += int(K)*-1
  K = 0
print(sum)

Submission status: Accepted

Solution code:
Z = list(map(int, input().split()))
K=Z[-1]
A=Z[0]
B=Z[1]
C=Z[2]
k=K
ans=0
if k>A+B:
  ans=A*2-K+B
elif k>=A:
  ans=A
else:
  ans=k
  
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

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

Submission status: Accepted

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

if K < A:#KがAより小さければ最大値はK
    max = K
else:
    if K < A+B:#KがA+Bより小さければ最大値はA
        max = A
    else:
        if K < A+B+C:#KがA+B+Cより小さければ最大値はA-(K-A-B)
            max = A + B*0 + (K-A-B)*-1
        else:
            max = A + B*0 + C*(-1)

print(max)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
inputs = [int(x) for x in input().split()]
sumAns = 0

if inputs[0] > inputs[3]:
	sumAns = sumAns + inputs[3]
	inputs[3] = 0
else:
	sumAns = sumAns + inputs[0]
	inputs[3] = inputs[3] - inputs[0]

if inputs[1] > inputs[3]:
	inputs[3] = 0
else:
	inputs[3] = inputs[3] - inputs[1]

if inputs[2] > inputs[3]:
	sumAns = sumAns - inputs[3]
	inputs[3] = 0
else:
	sumAns = sumAns - inputs[3]
	inputs[3] = inputs[3] - inputs[2]

print(sumAns)

Submission status: Accepted

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

print(x)

Submission status: Accepted

Solution code:
def F(A,B,C):
  return A-C;
[A,B,C,K]=input().split();
A=int(A);
B=int(B);
C=int(C);
K=int(K);
a=min(A,K);
b=min(K-a,B);
c=K-a-b

print(F(a,b,c));

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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

max_sum = 0

if a > k:
    max_sum = k
elif a+b >= k:
    max_sum = a
else:
    max_sum = a*2+b-k

print(max_sum)

Submission status: Accepted

Solution code:
import sys

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

if a < l:
    sm += a
    l -= a
else:
    sm += l
    print(sm)
    sys.exit()

if b < l:
    l -= b
else:
    print(sm)
    sys.exit()

sm -=  l
print(sm)

Submission status: Accepted

Solution code:
a, b, c, k = map(int, input().split())
chk1 = k-a
chk2 = k-a-b
if chk1 <= 0:
    ans = k

else:
    if chk2 > 0:
        ans = a -1*chk2
    else:
        ans = a

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
A,B,C,K = map(int,input().split())
sum = 0
if K>0:
    if A>0:
        if A>K:
            sum = sum + K
        else:
            sum = sum + A
    K = K - A
if K > 0:
    if B>0:
        K = K - B
if K > 0:
    if C>0:
        sum = sum + K * (-1)
print(sum)

Submission status: Accepted

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

backup = K
AB = A + B
flag = False
ans = 0
if K <= A:
    print(K)
else:
    if A < K:
        K = K - A
        ans = ans + (A * 1)
    if B <= K:
        K = K - B
        flag = True
    if K > 0 and flag:
        ans = ans + (-1) * (backup - AB)
    print(ans)

Submission status: Accepted

Solution code:
a,b,c,k = map(int,input().split())
aa=a
if k == 0:
    print(0)
elif a > k:
    print(k)
elif a+b >= k:
    print(a)
else:
    ab = a+b
    ans = aa-(k-ab)
    print(ans)

Submission status: Accepted

Solution code:
A,B,C,K=input().split()
res=0
cnt=0
if int(K)-int(A)<=0:
  		res=1*int(K)
else:
  		res=1*int(A)
cnt=int(K)-int(A)-int(B)
if cnt>0:
        res-=1*cnt
print(res)

Submission status: Accepted

Solution code:
a, b, c, k = (int(x) for x in input().split())
if k <= a:
  max = k
elif k <= (a+b):
  max = a
else:
  max = k if k <= a else a-1*(k-(a+b))
print(max)

Submission status: Accepted

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

Submission status: Accepted

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

#print(a,b,c,k)
sum = 0
flag = -1

if a>=k:
  flag = 1
  sum = k
else:
  sum = a
  k = k-a

if flag==-1:
  if b>=k:
    flag = 1
  else:k = k-b

if flag == -1:
  if c>=k:sum = sum -k
  else: sum = sum -c
    
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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 and k <= a  + b + c:
    print(a-k+a+b)

Submission status: Accepted

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

Submission status: Accepted

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

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