AtCoder Beginner Contest 158 - Count Balls

Takahashi has many red balls and blue balls. Now, he will place them in a row.
Initially, there is no ball placed.
Takahashi, who is very patient, will do the following operation 10^{100} times:
 - Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.
How many blue balls will be there among the first N balls in the row of balls made this way?

-----Constraints-----
 - 1 \leq N \leq 10^{18}
 - A, B \geq 0
 - 0 < A + B \leq 10^{18}
 - All values in input are integers.

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

-----Output-----
Print the number of blue balls that will be there among the first N balls in the row of balls.

-----Sample Input-----
8 3 4

-----Sample Output-----
4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Source: atcoder | Difficulty: EASY


Solutions:
(n, a, b) = map(int, input().split())
m = n // (a + b)
print(m * a + min(a, n - m * (a + b)))


(n, a, b) = map(int, input().split())
ans = a * (n // (a + b)) + min(n % (a + b), a)
print(ans)


def main():
	(n, a, b) = map(int, input().split())
	f = a * (n // (a + b))
	l = n % (a + b)
	rest = 0
	if l < a:
		rest = l
	else:
		rest = a
	return f + rest

def __starting_point():
	print(main())
__starting_point()


(N, A, B) = map(int, input().split())
sho = N // (A + B)
amari = N % (A + B)
if amari >= A:
	ans = sho * A + A
else:
	ans = sho * A + amari
print(ans)


(N, A, B) = map(int, input().split())
print(N // (A + B) * A + min(A, N % (A + B)))


(n, a, b) = map(int, input().split())
s = a + b
print(n // s * a + min(a, n % s))


(n, blue, red) = list(map(int, input().split()))
quot = n // (blue + red)
rem = n % (blue + red)
ans = blue * quot + min(blue, rem)
print(ans)


(N, A, B) = list(map(int, input().split()))
ans = N // (A + B) * A
ans += min(N % (A + B), A)
print(ans)


(n, a, b) = map(int, input().split())
answer = n // (a + b) * a + min(n % (a + b), a)
print(answer)


(n, a, b) = map(int, input().split())
print(n // (a + b) * a + a if n - n // (a + b) * (a + b) >= a else n - n // (a + b) * b)


(n, a, b) = map(int, input().split())
(d, m) = divmod(n, a + b)
print(d * a + min(m, a))


(n, a, b) = list(map(int, input().split()))
print(n // (a + b) * a + min(n % (a + b), a))


(N, A, B) = map(int, input().split())
a = N // (A + B) * A
c = N % (A + B)
b = min(A, c)
print(a + b)


(x, b, r) = map(int, input().split())
k = int(x / (b + r))
print(k * b + min(x % (b + r), b))


(n, a, b) = map(int, input().split())
i = a + b
div = n // i
rm = n % i
print(div * a + min(rm, a))


(n, a, b) = map(int, input().split())
x = n // (a + b)
y = n % (a + b)
z = min(a, y)
print(a * x + z)


(n, a, b) = map(int, input().split())
x = n // (a + b)
c = n - (a + b) * x
print(int(a * x + min(c, a)))


(N, A, B) = map(int, input().split())
div = N // (A + B)
mod = N % (A + B)
print(div * A + min(mod, A))


(a, b, c) = map(int, input().split())
print(a // (b + c) * b + min(b, a % (b + c)))


(N, A, B) = map(int, input().split())
d = N // (A + B)
ans = 0
ans += A * d
ans += min(N - (A + B) * d, A)
print(ans)


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


(n, a, b) = map(int, input().split())
ans = 0
b1 = n // (a + b) * a
if n % (a + b) >= a:
	print(b1 + a)
else:
	b2 = n % (a + b)
	print(b1 + b2)


(n, b, c) = map(int, input().split())
print(n // (b + c) * b + min(b, n % (b + c)))


(n, a, b) = map(int, input().split())
f = n // (a + b)
print(a * f + min(a, n % (a + b)))


(N, A, B) = map(int, input().split())
d = N // (A + B)
z = N % (A + B)
print(d * A + min(z, A))


(N, A, B) = map(int, input().split())
div = N // (A + B)
rm = N % (A + B)
print(div * A + min(rm, A))


(n, a, b) = map(int, input().split())
(x, y) = divmod(n, a + b)
print(x * a + min(a, y))


(n, a, b) = map(int, input().split())
(d, m) = divmod(n, a + b)
if a < m:
	m = a
print(d * a + m)


(n, a, b) = map(int, input().split())
w = n // (a + b) * a
w += min(n % (a + b), a)
print(w)


(n, a, b) = map(int, input().split())
ab = a + b
x1 = n // ab * a
x2 = min(n % ab, a)
print(x1 + x2)


(n, a, b) = map(int, input().split())
(m, l) = divmod(n, a + b)
print(m * a + min(l, a))


(n, a, b) = (int(x) for x in input().split())
y = n // (a + b) * a + min(n % (a + b), a)
print(y)


(N, A, B) = map(int, input().split())
(r, d) = divmod(N, A + B)
print(A * r + min(A, d))


(N, A, B) = list(map(int, input().split()))
M = N // (A + B)
L = N % (A + B)
if L > A:
	L = A
print(A * M + L)


(N, A, B) = map(int, input().split())
C = A + B
ans = A * (N // C)
ans += min(N % C, A)
print(ans)


(n, a, b) = map(int, input().split())
su = a + b
print(n // su * a + min(n - n // su * su, a))


(n, a, b) = (int(i) for i in input().split())
print(n // (a + b) * a + min(n % (a + b), a))


(N, A, B) = map(int, input().split())
(q, r) = divmod(N, A + B)
print(q * A + min(r, A))


(n, a, b) = map(int, input().split())
k = a + b
x = n // k
print(a * x + min(a, n - k * x))


(N, A, B) = list(map(int, input().split()))
sets = N // (A + B)
a_count = A * sets
mod = N % (A + B)
a_count += mod if mod < A else A
print(a_count)


(n, a, b) = map(int, input().split())
c = a + b
quo = n // c
mod = n % c
ans = a * quo + min(mod, a)
print(ans)


(n, a, b) = list(map(int, input().split()))
c = a + b
div = n // c
mod = n % c
if mod > a:
	mod = a
ans = div * a + mod
print(ans)


(n, a, b) = list(map(int, input().split()))
x = n % (a + b)
y = n // (a + b)
if x > a:
	print(a * (y + 1))
else:
	print(a * y + x)


(n, a, b) = map(int, input().split())
q = n // (a + b)
r = n % (a + b)
print(q * a + min(a, r))


(N, A, B) = map(int, input().split(' '))
ans = 0
if A + B > 0:
	ans = N // (A + B) * A + min(A, N % (A + B))
print(ans)


(n, a, b) = map(int, input().split())
c = n // (a + b)
d = min(n - (a + b) * c, a)
print(c * a + d)


(n, a, b) = map(int, input().split())
ans = 0
x = n // (a + b)
y = n % (a + b)
if y < a:
	print(x * a + y)
else:
	print(x * a + a)


(S, A, B) = map(int, input().split())
print(S // (A + B) * A + min(A, S % (A + B)))


(n, a, b) = [int(e) for e in input().split()]
print(n // (a + b) * a + min(n % (a + b), a))


def resolve():
	(n, a, b) = map(int, input().split())
	print(n // (a + b) * a + min(n % (a + b), a))
resolve()


(n, b, r) = map(int, input().split())
print(n // (b + r) * b + min(b, n % (b + r)))


(N, A, B) = map(int, input().split())
(times, residue) = divmod(N, A + B)
blue = times * A + min(residue, A)
print(blue)


(n, a, b) = map(int, input().split())
t = n % (a + b)
print(n // (a + b) * a + min(a, t))


(N, A, B) = map(int, input().split())
answer = 0
if A == 0:
	print(0)
	return
C = A + B
D = N // C
F = N % C
if F >= A:
	print((D + 1) * A)
else:
	print(D * A + F)


(n, a, b) = map(int, input().split())
q = n // (a + b)
r = n % (a + b)
if r >= a:
	res = a
else:
	res = r
print(q * a + res)


(n, a, b) = list(map(int, input().split()))
cnt = n // (a + b)
rem = n - cnt * (a + b)
ans = cnt * a
if rem > a:
	ans += a
else:
	ans += rem
print(ans)


(N, A, B) = map(int, input().split())
s = A + B
r = N // s
b = N % s if N % s < A else A
print(r * A + b)


(N, A, B) = map(int, input().split())
print(A * (N // (A + B)) + (A if N % (A + B) > A else N % (A + B)))


(N, A, B) = map(int, input().split())
(s, d) = divmod(N, A + B)
print(A * s + min(d, A))


(N, A, B) = map(int, input().split())
p = N // (A + B)
q = N % (A + B)
print(A * p + min(q, A))


(n, a, b) = map(int, input().split())
l = n // (a + b)
r = n % (a + b)
print(l * a + min(r, a))


(n, a, b) = map(int, input().split())
ct = a * (n // (a + b))
x = n % (a + b)
ct += min(a, x)
print(ct)


(N, A, B) = map(int, input().split())
Q = N // (A + B)
R = N % (A + B)
print(Q * A + min(A, R))


(n, a, b) = map(int, input().split())
rem = n % (a + b)
print(n // (a + b) * a + min(rem, a))


def main():
	(N, A, B) = list(map(int, input().split()))
	quo = N // (A + B)
	rem = N % (A + B)
	if rem > A:
		rem = A
	ans = quo * A + rem
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, a, b) = list(map(int, input().split()))
line = n // (a + b)
remain = n % (a + b)
ans = line * a
ans += min(a, remain)
print(ans)


(n, a, b) = map(int, input().split())
res = n // (a + b)
print(res * a + min(n - res * (a + b), a))


(n, a, b) = map(int, input().split())
k = n // (a + b)
l = n - k * (a + b)
print(k * a + min(a, l))


(N, A, B) = map(int, input().split())
(D, M) = divmod(N, A + B)
print(D * A + min(M, A))


(N, A, B) = map(int, input().split())
x = N // (A + B)
y = N % (A + B)
print(A * x + min(A, y))


(N, A, B) = map(int, input().split())
c = N // (A + B)
d = N % (A + B)
print(c * A + min(d, A))


(n, a, b) = map(int, input().split())
(x, y) = divmod(n, a + b)
if y > a:
	y = a
print(x * a + y)


n = input()
n = n.split()
a = int(n[1])
b = int(n[2])
n = int(n[0])
if n % (a + b) < a:
	print(int(n / (a + b)) * a + n % (a + b))
else:
	print(int(n / (a + b)) * a + a)


(n, a, b) = map(int, input().split())
ns = n % (a + b)
if a == 0:
	print(0)
else:
	print(1 * min(ns, a) + a * (n // (a + b)))


(n, a, b) = map(int, input().split())
blue = n // (a + b) * a + min(a, n % (a + b))
print(blue)


(n, a, b) = map(int, input().split())
words_b = a
l_a_b = a + b
if a == 0 and b == 0:
	print('0')
	return
(d, m) = divmod(n, a + b)
if m == 0:
	print(words_b * d)
	return
if m <= a:
	print(int(words_b) * d + m)
else:
	print(int(words_b) * d + a)


(n, a, b) = map(int, input().split())
(div, mod) = divmod(n, a + b)
ans = div * a + min(mod, a)
print(ans)


(N, A, B) = list(map(int, input().split()))
x = N // (A + B)
y = N % (A + B)
if y <= A:
	print(str(A * x + y))
else:
	print(str(A * x + A))


(N, A, B) = list(map(int, input().split()))
C = int(N / (A + B))
D = N % (A + B)
ans = 0
if 0 <= D <= A:
	ans = A * C + D
else:
	ans = A * C + A
print(ans)


import os, sys, re, math
(N, A, B) = [int(n) for n in input().split()]
S = A + B
print(N // S * A + min(N % S, A))


(N, A, B) = map(int, input().split())
C = N // (A + B)
D = N % (A + B)
print(C * A + min(D, A))


(N, A, B) = list(map(int, input().split()))
if A == 0 and B == 0:
	print(0)
elif A == 0:
	print(0)
elif B == 0:
	print(N)
else:
	(q, mod) = divmod(N, A + B)
	print(min(mod, A) + q * A)


(N, A, B) = map(int, input().split())
ans = N // (A + B) * A
nokori = N % (A + B)
ans += min(A, nokori)
print(ans)


import math

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

def i_map():
	return 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)]
(n, a, b) = i_map()
ans = n // (a + b) * a
ans += min(n % (a + b), a)
print(ans)


(N, A, B) = map(int, input().split())
apb = A + B
ans = A * (N // apb)
N = N % apb
if N > A:
	ans += A
else:
	ans += N
print(ans)


(n, a, b) = map(int, input().split())
A = n // (a + b)
B = n % (a + b)
print(a * A + min(a, B))


(N, A, B) = map(int, input().split())
(d, m) = divmod(N, A + B)
print(d * A + min(m, A))


(n, a, b) = map(int, input().split())
r = a * (n // (a + b))
q = n % (a + b)
if q > a:
	q = a
print(r + q)


(N, A, B) = map(int, input().split(' '))
ans = 0
if A + B > 0:
	taple = divmod(N, A + B)
	ans = taple[0] * A + min(A, taple[1])
print(ans)


(N, A, B) = map(int, input().split())
ser = N // (A + B)
mod = N % (A + B)
print(ser * A + min(mod, A))


(n, b, r) = list(map(int, input().split()))
m = n // (b + r)
last = min(n - (b + r) * m, b)
print(b * m + last)


(N, A, B) = list(map(int, input().split()))
M = N % (A + B)
ans = N // (A + B)
print(ans * A + A) if M >= A else print(ans * A + M)


(n, a, b) = map(int, input().split())
p = n // (a + b)
q = n % (a + b)
print(p * a + min(q, a))


(n, a, b) = list(map(int, input().split()))
c = n // (a + b)
if a < n % (a + b):
	print(c * a + a)
else:
	print(c * a + n % (a + b))


import sys
input = sys.stdin.readline

def main():
	(n, a, b) = map(int, input().split())
	a_count = 0
	(d, r) = divmod(n, a + b)
	a_count += a * d
	if r > a:
		a_count += a
	else:
		a_count += r
	print(a_count)

def __starting_point():
	main()
__starting_point()


(N, A, B) = list(map(int, input().split()))
S = A + B
print(N // S * A + min(A, N % S))


(n, a, b) = map(int, input().split())
k = n % (a + b)
if k >= a:
	k = a
print(n // (a + b) * a + k)


(n, a, b) = map(int, input().split())
if a == 0:
	print(0)
	return
syou = n // (a + b)
amari = n % (a + b)
if amari <= a:
	print(syou * a + amari)
else:
	print(syou * a + a)


(n, a, b) = map(int, input().split())
t = n // (a + b)
ans = t * a
s = n % (a + b)
if s <= a:
	ans += s
else:
	ans += a
print(ans)


(N, A, B) = map(int, input().split())
sum = 0
sum += A * (N // (A + B))
if N % (A + B) > A:
	sum += A
else:
	sum += N % (A + B)
print(sum)


(a, b, c) = list(map(int, input().split()))
d = a // (b + c) * b + min(a % (b + c), b)
print(d)


(N, A, B) = map(int, input().split())
(sho, ama) = divmod(N, A + B)
print(A * sho + min(ama, A))


(N, A, B) = map(int, input().split())
s = N % (A + B) > A
t = not s
print(A * (N // (A + B)) + s * A + t * N % (A + B))


(n, a, b) = map(int, input().split())
p = n // (a + b)
q = n % (a + b)
if q <= a:
	amari = q
else:
	amari = a
if a == 0:
	print(0)
elif q <= a:
	print(a * p + amari)
else:
	print(a * p + amari)


def main():
	(N, A, B) = tuple([int(_x) for _x in input().split()])
	turn = A + B
	iteration = N // turn
	rest = N - iteration * turn
	restb = min(A, rest)
	print(A * iteration + restb)
main()


(n, a, b) = map(int, input().split())
c = a + b
s = n // c
t = n % c
print(s * a + min(a, t))


(n, a, b) = map(int, input().split(' '))
(q, r) = divmod(n, a + b)
s = q * a
if r > a:
	s += a
else:
	s += r
print(s)


(n, a, b) = map(int, input().split())
s = n // (a + b)
r = n % (a + b)
if r <= a:
	print(s * a + r)
elif r > a:
	print(s * a + a)


(N, A, B) = map(int, input().split())
l = A + B
(c, r) = divmod(N, l)
if r > A:
	r = A
print(A * c + r)


(n, a, b) = map(int, input().split())
c = n // (a + b)
j = n % (a + b)
print(c * a + min(j, a))


(n, a, b) = map(int, input().split())
(r, p) = divmod(n, a + b)
print(r * a + min([p, a]))


(n, a, b) = [int(i) for i in input().split()]
if a + b == 0:
	print(0)
	return
division = n // (a + b)
surplus = n % (a + b)
print(division * a + min(a, surplus))


(N, A, B) = map(int, input().split(' '))
ans = 0
if A + B != 0:
	taple = divmod(N, A + B)
	ans = taple[0] * A
	if B == 0:
		ans = N
	elif taple[1] > A:
		ans += A
	else:
		ans += taple[1]
print(ans)


(n, a, b) = map(int, input().split())
ab = a + b
print(n // ab * a + min(n - n // ab * ab, a))


(N, A, B) = map(int, input().split())
a = 0
t = N % (A + B)
a += N // (A + B) * A
a += min(t, A)
print(a)


(N, A, B) = map(int, input().split())
n1 = N // (A + B)
n2 = N % (A + B)
if n2 > A:
	print(A * (n1 + 1))
else:
	print(A * n1 + n2)


(n, a, b) = map(int, input().split())
x = n // (a + b)
y = n % (a + b)
ans = a * x + min(y, a)
print(ans)


(N, A, B) = map(int, input().split())
r = N // (A + B) * A
s = N % (A + B)
x = r + min(A, s)
print(x)


(N, A, B) = map(int, input().split())
ans = N // (A + B) * A
X = N % (A + B)
ans += min(X, A)
print(ans)


(N, A, B) = map(int, input().split())
ans = N // (A + B) * A
left = N % (A + B)
if left <= A:
	print(ans + left)
else:
	print(ans + A)


(n, a, b) = [int(j) for j in input().split()]
print(a * (n // (a + b)) + min(n % (a + b), a))


(n, a, b) = map(int, input().split())
(p, q) = divmod(n, a + b)
print(p * a + [a, q][q < a])


(n, a, b) = map(int, input().split())
k = n // (a + b)
h = min(a, n % (a + b))
print(a * k + h)


def __starting_point():
	(N, A, B) = map(int, input().split())
	S = (A + B) * 1000
	ans = 0
	if N > A + B:
		ans = A * (N // (A + B))
		if A > N % (A + B):
			ans += N % (A + B)
		else:
			ans += A
	elif N < A + B:
		if A > N:
			ans = N
		else:
			ans = A
	else:
		ans = A
	print(ans)
__starting_point()


def answer(n: int, a: int, b: int) -> int:
	(q, mod) = divmod(n, a + b)
	return q * a + min(a, mod)

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

def __starting_point():
	main()
__starting_point()


(n, a, b) = list(map(int, input().split()))
k = n // (a + b)
p = n % (a + b)
print(a * k + min(a, p))


(n, b, r) = map(int, input().split())
p = n // (b + r) * b
q = n % (b + r)
s = min(b, q)
print(p + s)


(n, a, b) = map(int, input().split())
cnt = n // (a + b) * a
n = n % (a + b)
cnt += min(n, a)
print(cnt)


def main():
	(n, a, b) = map(int, input().split())
	quo = n // (a + b)
	rem = n % (a + b)
	ans = quo * a
	if a >= rem:
		ans += rem
	else:
		ans += a
	print(ans)

def __starting_point():
	main()
__starting_point()