AtCoder Beginner Contest 167 - Easy Linear Programming

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.
We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

-----Constraints-----
 - All values in input are integers.
 - 0 \leq A, B, C
 - 1 \leq K \leq A + B + C \leq 2 \times 10^9

-----Input-----
Input is given from Standard Input in the following format:
A B C K

-----Output-----
Print the maximum possible sum of the numbers written on the cards chosen.

-----Sample Input-----
2 1 1 3

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

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Source: atcoder | Difficulty: EASY


Solutions:
(A, B, C, K) = list(map(int, input().split()))
(a, c) = (min(A, K), max(0, min(C, K - A - B)))
print(a - c)


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


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


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


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


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


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


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


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


def i_input():
	return int(input())

def i_map():
	return list(map(int, input().split()))

def i_list():
	return list(map(int, input().split()))

def i_row(N):
	return [int(input()) for _ in range(N)]

def i_row_list(N):
	return [list(map(int, input().split())) for _ in range(N)]
(a, b, c, k) = i_map()
if k <= a:
	print(k)
elif k <= a + b:
	print(a)
else:
	print(a - (k - a - b))


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


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


(a, b, c, k) = map(int, input().split())
print(min(2 * a + b - k, a, k))


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


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


(A, B, C, K) = map(int, input().split())
r = min(A, K)
K -= r
K -= min(B, K)
r -= K
print(r)


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


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


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


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

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

def __starting_point():
	main()
__starting_point()


(A, B, C, K) = [int(s) for s in input().split()]
print(min(A, K) - min(C, max(K - (A + B), 0)))


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


[A, B, C, K] = list(map(int, list(input().split(' '))))
left = K
ans = 0
left = K - A
if left >= 0:
	ans += A
else:
	ans += K
left = left - B
if left > 0:
	ans -= left
print(ans)


(A, B, C, K) = map(int, input().split())
num_A = min(A, K)
num_B = min(B, K - num_A)
num_C = min(C, K - num_A - num_B)
print(num_A - num_C)


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


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


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


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


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


(A, B, C, K) = [int(_) for _ in input().split()]
u = min(A, K)
v = max(0, K - A - B)
print(u - v)


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


(A, B, C, K) = map(int, input().split())
a = min(K, A)
K -= a
b = min(K, B)
K -= b
c = K
print(a - c)


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


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


import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp
INF = inf
MOD = 1000000007
(a, b, c, k) = [int(i) for i in input().split()]
tmp = 0
res = 0
if k <= a:
	res = k
elif k <= a + b:
	res = a
else:
	res = 2 * a + b - k
print(res)


(a, b, c, k) = list(map(int, input().split()))
s = []
x = 0
if k <= a:
	print(k)
elif k <= a + b:
	print(a)
elif k <= a + b + c:
	print(2 * a + b - k)
else:
	print(a - c)


(A, B, C, K) = map(int, input().split())
X = min(K, A)
ans = X
K -= X
K -= min(K, B)
print(ans - min(K, C))


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

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


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


(a, b, c, k) = map(int, input().split())
s = 1 * min(a, k)
k = k - min(a, k)
k = k - min(b, k)
s -= k
print(s)


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


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


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


def __starting_point():
	S = input()
	(a, b, c, k) = map(int, S.split(sep=' '))
	if k <= a:
		print(k)
	elif k <= a + b:
		print(a)
	elif k <= a + b + c:
		print(a - (k - a - b))
__starting_point()


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


(a, b, c, x) = map(int, input().split())
ans = 0
ans += min(a, x)
x -= min(a, x)
x -= min(b, x)
ans -= min(c, x)
x -= min(c, x)
print(ans)


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


(a, b, c, k) = [int(i) for i in input().split()]
print(min(a, k) - max(0, k - a - b))


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


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


(a, b, c, z) = (int(i) for i in input().split())
res = min(a, z) - max(0, z - a - b)
print(res)


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


(a, b, c, k) = [int(x) for x in input().split()]
print(min(2 * a + b - k, a, k))


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


(A, B, C, K) = [int(x) for x in input().split()]
S = min(K, A)
K -= A
if K > 0:
	K -= B
if K > 0:
	S -= K
print(S)


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


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


(a, b, c, K) = map(int, input().split())
print(K if a >= K else a if b + a >= K else a - (K - a - b))


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


line = input().split(' ')
a = int(line[0])
b = int(line[1])
c = int(line[2])
k = int(line[3])
ans = 0
if a >= k:
	ans = k
else:
	ans += a
	k -= a
	if b < k:
		k -= b
		ans -= k
print(ans)


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


(one, zero, neg_one, k) = map(int, input().split())
print(min(one, k) - max(0, k - one - zero))


(A, B, C, K) = list(map(int, input().split()))
if A + B > K:
	print(K)
	exit()
print(A - (K - (A + B)))


(a, b, c, k) = map(int, input().split())
A = min(a, k)
k -= A
B = min(b, k)
k -= B
print(A - k)


(A, B, C, K) = [int(i) for i in input().split()]
import sys
if A >= K:
	print(K)
	return
r1 = K - A
cnt = A
if B >= r1:
	print(cnt)
	return
r2 = r1 - B
print(cnt - r2)


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


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


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


import sys

def input():
	return sys.stdin.readline().rstrip()
(A, B, C, K) = map(int, input().split())
point = 0
if A <= K:
	point += A
	K -= A
else:
	point += K
	K = 0
if B <= K:
	K -= B
else:
	K = 0
point -= K
print(point)


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


(A, B, C, K) = map(int, input().split())
A = min(A, K)
B = min(K - A, B)
C = min(K - A - B, C)
assert A + B + C >= K
print(A - C)


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


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


(a, b, c, k) = map(int, input().split())
x = min(a, k)
y = min(b, k - a)
print(x + (k - a - y) * -1)


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

def __starting_point():
	main()
__starting_point()


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


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


(a, b, c, k) = map(int, input().split())
xa = min(a, k)
k -= xa
xb = min(b, k)
k -= xb
xc = min(c, k)
print(xa - xc)


(p, z, n, k) = map(int, input().split())
print(min(k, p) if k <= p + z else p - (k - (p + z)))


(A, B, C, K) = map(int, input().split())
P = min(A, K)
Q = min(B, K - P)
R = K - P - Q
print(P - R)


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


(a, b, c, k) = map(int, input().split())
ax = min(a, k)
k = k - ax
bx = min(b, k)
k = k - bx
cx = k
print(ax - cx)


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


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


[A, B, C, K] = [int(i) for i in input().split()]
j1 = A - K
j2 = A + B - K
if j2 > 0:
	if j1 < 0:
		print(A)
	else:
		print(K)
else:
	print(A + j2)


def main():
	(a, b, c, k) = list(map(int, input().split()))
	sum = 0
	n = k - a
	if n <= 0:
		sum = k
		print(sum)
		return
	else:
		sum = a
	n = n - b
	if n <= 0:
		print(sum)
		return
	n = n - c
	if n <= 0:
		sum = sum - (k - (a + b))
		print(sum)
		return
main()


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


(a1, a0, am, n) = map(int, input().split())
print(min(a1, n) - min(am, max(0, n - a1 - a0)))


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


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


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


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