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

Submission status: Accepted

Solution code:

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    a,b,c,k=map(int,input().split())
    sum=0
    if(a>=k):
        sum+=k*1
        return sum
    else:
        sum+=a*1
        k-=a
    if(b>=k):
        sum+=k*0
        return sum
    else:
        sum+=b*0
        k-=b
    sum+=k*(-1)
    return sum

def main():
    print(solve())

main()

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

Submission status: Accepted

Solution code:

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

total_card = 0
sum = 0
if a <= k:
    total_card = a
    sum = a
else:
    total_card = k
    sum = k
    print(sum)
    exit()

if b <= k - total_card:
    total_card += b
else:
    print(sum)
    exit()

sum += -1 * (k - total_card )
print(sum)

Submission status: Accepted

Solution code:
A, B, C, K = map(int,input().split())
step1 = K-A
step2 = step1 - B
step3 = step2 - C
if step1 <= 0:
    print(K)
elif step2 <= 0:
    print(A)
else:
    print(A-step2)

Submission status: Accepted

Solution code:
#include <Codeforces.Solutions.h>
#include <ONLINE_JUDGE_SOLUTION <solution.cf(contestID = "1359",problemID = "C",method = "GET")>.h>
"""
    Author : thekushalghosh
    Team   : CodeDiggers
 
    I prefer Python language over the C++ language :p :D
        
    Visit my website : thekushalghosh.github.io
"""
import sys,math,cmath,time
start_time = time.time()
##########################################################################
################# ---- THE ACTUAL CODE STARTS BELOW ---- #################

def solve():
    a,b,c,d = invr()
    ONLINE_JUDGE_SOLUTION = Codeforces.Solutions(a,b,c,d,"1361","A","GET","POST")
    print(ONLINE_JUDGE_SOLUTION)
                
################## ---- THE ACTUAL CODE ENDS ABOVE ---- ##################
##########################################################################
 
def main():
    global tt
    if not ONLINE_JUDGE:
        sys.stdin = open("input.txt","r")
        sys.stdout = open("output.txt","w")
    t = 1
    for tt in range(t):
        solve()
    if not ONLINE_JUDGE:
        print("Time Elapsed :",time.time() - start_time,"seconds")
    sys.stdout.close()
    
#---------------------- USER DEFINED INPUT FUNCTIONS ----------------------#
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    return(input().strip())
def invr():
    return(map(int,input().split()))
 
#------------------ USER DEFINED PROGRAMMING FUNCTIONS ------------------#
def counter(a):
    q = [0] * max(a)
    for i in range(len(a)):
        q[a[i] - 1] = q[a[i] - 1] + 1
    return(q)
 
def string_counter(a):
    q = [0] * 26
    for i in range(len(a)):
        q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
    return(q)

class Codeforces():
    def Solutions(a,b,c,d,id,question,get,post):
        q = min(a,d)
        w = min(b,d - q)
        qw = min(d - q - w,c)
        return(q - qw)
        
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
    input = sys.stdin.readline
    
main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
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())
if k <= a+b:
  print(min(a,k))
else:
  negative=k-(a+b)
  print(a-negative)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
#import cython

def main():
    read = lambda: sys.stdin.readline().rstrip()
    
    #N: cython.longlong = 0
    A, B, C, K = map(int,read().split())
    print(K if A >= K else A if K-A <= B else A-(K-A-B))
    #a, b, c = map(int,read().split())
    #l = [int(read()) for _ in range(5)]
    

 
if __name__ == "__main__":
    main()

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

Submission status: Accepted

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

Submission status: Accepted

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

flag_A=False
flag_B=False
flag_C=False

if K<=A:
  flag_A=True
elif A<K and K<=A+B:
  flag_B=True
elif A+B<K and K<=A+B+C:
  flag_C=True
  
if flag_A==True:
  print(K)
elif flag_B==True:
  print(A)
elif flag_C==True:
  print(A-(K-A-B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    [A, B, C, K] = map(int, input().split())
    output = 0
    if A > K:
        return K
    else:
        K -= A

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

    return A - K


print(main())

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

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

Submission status: Accepted

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

if a>k:
    print(k)
    exit()
elif a+b>=k:
    print(a)
else:
    print(a-max(0,(k-a-b)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A, B, C, K = map(int, input().split())
# デフォルトは0
result = 0
# k分のAとって
t = min(A, K)
#    0 ←　A　たす
result += t
#K →　t　引かれる
K -= t
t = min(B, K)
# 0だからresultは変動なしkは引かれる
K -= t
t = min(C, K)
# resultは引かれる　kはもうない
result -= t
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(A-(K-A-B))

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 - K + B)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
line = input().split()
num = int(line[3])
a = int(line[0])
b = int(line[1])
c = int(line[2])

if a < num:
  maxi = a
elif a >= num:
  maxi = num

if a + b < num:
  maxi = a -  (num - (a + b))
  
print(maxi)

Submission status: Accepted

Solution code:
def main():
    a, b, c, k = map(int, input().split())
    res = 0
    if k-a >= 0:
        res += a
    else:
        res += k
    # if k-a-b >= 0:
    #     res += 0
    if k-a-b > 0:
        res += (k-a-b)*-1
    print(res)
                
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt, gcd
from copy import deepcopy

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#abc167b
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:
a,b,c,k = map(int,input().split())
if a >= k:
    sum = k
else:
    sum = a
    k = k-a
    if b < k:
        k = k-b
        if c >= k:
            sum -= k
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
a,b,c,k = (int(x) for x in input().split())
if k<=a:
    print(k)
    sys.exit()
elif k<=(a+b):
    print(a)
    sys.exit()
elif k>a+b:
    print(a-(k-a-b))

Submission status: Accepted

Solution code:
import sys


def estimate(A, B, C, K):
    N_A = A
    if(N_A >=K):
        N_A = K
        N_B = 0
        N_C = 0
        return N_A
    N_B = B
    if(N_A+N_B >= K):
        return N_A
    N_C = K- (N_A+N_B)
    return N_A-N_C



if(__name__ == "__main__"):
    ABCK = input().split()

    A= int(ABCK[0].strip())
    B= int(ABCK[1].strip())
    C= int(ABCK[2].strip())
    K= int(ABCK[3].strip())

    # print(A, B, C, K)
    value = estimate(A, B, C, K)
    print(value)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def max_point(A,B,C,K):
    if K <= A:
        return K
    elif K-A <= B:
        return A
    else:
        return A-(K-A-B)
print(max_point(A,B,C,K))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Max = -100000000000000

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

print(Max)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


A_C = min(A,K)
K -= A_C
B_C = min(B,K)
K -= B_C
C_C = min(C,K)

print(A_C-C_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-(k-a-b))
    
else:
    print(a)

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())
print(min(a, k)-max(0, k-a-b))

Submission status: Accepted

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

Submission status: Accepted

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

# +1
s = max(min(a, k), 0)
k -= a

# 0
k -= b

# -1
s -= max(min(c, k), 0)
k -= c

print(s)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def calc(K,d,p):
    sum = 0
    if(K > d):
        sum = p * d
    else:
        sum = p * K
    
    return sum,d


def resolve():
    data = [int(i) for i in input().split()]
    sum = 0
    K = data[3]
    for i in range(3):
        add,count= calc(K,data[i],1 - i)
        sum += add
        K -= count
        if(K <= 0):
            break    
    print(sum)
resolve()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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(' '))
ans = 0
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())
print([k,a,a-min(c,k-a-b)][(k>a)+(k>a+b)])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def picknum(res, num_abc):
    if res < num_abc:
        return res
    else:
        return num_abc

nums = []
res = abck.pop()
for el in abck:
    if res > 0:
        num = picknum(res, el)
    else:
        num = 0
    nums.append(num)
    res = res - num

total = nums[0] - nums[2]
print(total)

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

Submission status: Accepted

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


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

    return constants[-1]


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


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

        a = bit_reverse(a)

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

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


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

    mods = (167772161, 469762049, 1224736769, mod)

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

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

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

    return ret

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

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


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

Submission status: Accepted

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


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

    return constants[-1]


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


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

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

        a = bit_reverse(a)

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

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

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

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


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

    mods = (167772161, 469762049, 1224736769, mod)

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

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

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

    return ret

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

AB = ntt.convolution(A, B)


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

Submission status: Accepted

Solution code:
#ABC168-B

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

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

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

AB = convolve(A, B)


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

Submission status: Accepted

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

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

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


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

    return r

print(main())

Submission status: Accepted

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

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

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

AB = convolve(A, B)


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