zscoder wants to generate an input file for some programming competition problem.

His input is a string consisting of n letters 'a'. He is too lazy to write a generator so he will manually generate the input in a text editor.

Initially, the text editor is empty. It takes him x seconds to insert or delete a letter 'a' from the text file and y seconds to copy the contents of the entire text file, and duplicate it.

zscoder wants to find the minimum amount of time needed for him to create the input file of exactly n letters 'a'. Help him to determine the amount of time needed to generate the input.


-----Input-----

The only line contains three integers n, x and y (1 ≤ n ≤ 10^7, 1 ≤ x, y ≤ 10^9) — the number of letters 'a' in the input file and the parameters from the problem statement.


-----Output-----

Print the only integer t — the minimum amount of time needed to generate the input file.


-----Examples-----
Input
8 1 1

Output
4

Input
8 1 10

Output
8

Source: codeforces | Difficulty: HARD
Tags: Dynamic programming

Graph traversal


Solutions:
def fi(n):
	if n == 1:
		return x
	elif n == 2:
		return x + min(x, y)
	elif n % 2 == 1:
		return min(fi(n - 1), fi(n + 1)) + x
	else:
		return fi(n // 2) + min(y, x * (n // 2))
(n, x, y) = map(int, input().split())
print(fi(n))


(n, x, y) = list(map(int, input().split()))
import heapq
h = []
d = {}
heapq.heappush(h, (0, n))
r = -1
while r == -1:
	(xh, nh) = heapq.heappop(h)
	if nh not in d:
		d[nh] = xh
		if nh == 1:
			r = xh + x
		elif nh * x < y:
			r = xh + nh * x
		else:
			if nh - 1 not in d:
				heapq.heappush(h, (xh + x, nh - 1))
			if nh & 1 == 0 and nh // 2 not in d and (y < nh // 2 * x):
				heapq.heappush(h, (xh + y, nh // 2))
			if nh + 1 not in d:
				heapq.heappush(h, (xh + x, nh + 1))
print(r)


def recursion(n):
	if n == 1:
		return x
	if n == 2:
		return x + min(x, y)
	if n % 2 == 0:
		return recursion(n // 2) + min(y, x * (n - n // 2))
	else:
		return min(recursion(n + 1), recursion(n - 1)) + x
import sys
sys.setrecursionlimit(10000000)
(n, x, y) = list(map(int, input().split()))
print(recursion(n))


def main():

	def f(t):
		if t & 1:
			return x if t == 1 else min(f(t - 1), f(t + 1)) + x
		else:
			u = x * t
			return f(t // 2) + y if y * 2 < u else u
	(n, x, y) = map(int, input().split())
	print(f(n))
main()


import sys
sys.setrecursionlimit(1000000)

def f(dp, x, y, n):
	if dp[n] != -1:
		return dp[n]
	if n % 2 == 1:
		ans = x + min(f(dp, x, y, n - 1), f(dp, x, y, n + 1))
	else:
		ans = f(dp, x, y, n // 2) + min(y, x * n // 2)
	dp[n] = ans
	return ans
(n, x, y) = map(int, input().split())
dp = [-1 for i in range(n + 10)]
dp[1] = x
print(f(dp, x, y, n))


(n, x, y) = map(int, input().split(' '))
l = [0.0] * (n + 1)
for i in range(1, n + 1):
	l[i] = min(l[i - 1] + x, l[(i + 1) // 2] + y + x * (i & 1))
print(int(l[n]))


import sys
(n, x, y) = map(int, input().split())
size = 10 ** 7
(dq_x, dq_y, dq_i) = ([0] * size, [0] * size, [0] * size)
(l, r) = (0, 0)
(cx, cy) = (0, 0)
for i in range(1, n + 1):
	cx += 1
	while l < r and dq_i[l] < i:
		l += 1
	if l < r and (dq_x[l] - i - cx) * x + (dq_y[l] - cy) * y < 0:
		cx = dq_x[l] - i
		cy = dq_y[l]
	(tx, ty) = (cx + i * 2, cy + 1)
	while l < r and (dq_x[r] - tx) * x + (dq_y[r] - ty) * y > 0:
		r -= 1
	dq_x[r] = tx
	dq_y[r] = ty
	dq_i[r] = i * 2
	r += 1
print(cx * x + cy * y)


def main():

	def f(t):
		u = cache.get(t)
		if u is None:
			if t & 1:
				u = min(f(t - 1), f(t + 1)) + x
			else:
				u = x * t
				if y * 2 < u:
					u = f(t // 2) + y
			cache[t] = u
		return u
	(n, x, y) = list(map(int, input().split()))
	cache = {1: x}
	print(f(n))

def __starting_point():
	main()
__starting_point()


(n, a, b) = input().split(' ')
n = int(n)
a = int(a)
b = int(b)
tk = tk2 = dk = dk2 = hg = hg2 = tag = tag2 = k1 = k2 = 0
kg = n
while kg >= 2:
	if kg % 2 != 0:
		tag = 1
		if tag == 1 and (kg - kg // 2 - 1) * a < b:
			break
		hg2 = kg
		hg = (kg + 1) // 2
		while hg >= 2:
			if hg % 2 != 0:
				tag2 = 1
			if tag2 == 1 and (hg - hg // 2 - 1) * a < b:
				break
			if tag2 == 0 and (hg - hg // 2) * a < b:
				break
			if hg % 2 != 0:
				tk2 += 1
			hg //= 2
			tag2 = 0
			tk += 1
		t0 = b * tk + a * hg + a * tk2
		tag2 = 0
		hg = (hg2 - 1) // 2
		while hg >= 2:
			if hg % 2 != 0:
				tag2 = 1
			if tag2 == 1 and (hg - hg // 2 - 1) * a < b:
				break
			if tag2 == 0 and (hg - hg // 2) * a < b:
				break
			if hg % 2 != 0:
				dk2 += 1
			hg //= 2
			tag2 = 0
			dk += 1
		t1 = b * dk + a * hg + a * dk2
		if t0 <= t1:
			kg += 1
		else:
			kg -= 1
		tk = tk2 = dk = dk2 = hg2 = 0
	if tag == 1:
		k2 += 1
	if (kg - kg // 2) * a <= b:
		break
	kg //= 2
	tag = 0
	k1 += 1
t = b * k1 + k2 * a + kg * a
print(t)


import sys
sys.setrecursionlimit(1000000)

def f(n):
	if n == 1:
		return x
	elif n == 2:
		return x + min(x, y)
	elif n % 2 == 0:
		return f(n // 2) + min(y, x * (n - n // 2))
	else:
		return min(f(n + 1), f(n - 1)) + x
(n, x, y) = map(int, input().split())
print(f(n))


import sys
sys.setrecursionlimit(100000)
from functools import lru_cache

@lru_cache()
def best(a):
	if a == 1:
		return x
	elif a > 0:
		if a == 2:
			return x + min(x, y)
		elif a % 2 == 0:
			return best(a // 2) + min(y, (a - a // 2) * x)
		else:
			return min(best(a - 1), best(a + 1)) + x
(n, x, y) = map(int, input().split())
print(best(n))


__author__ = 'Think'
(n, x, y) = [int(i) for i in input().split()]

def worth(num):
	if num % 2 == 0:
		dub = y
		alt = x * (num // 2)
	else:
		dub = y + x
		alt = x * (num // 2 + 1)
	return (dub < alt, dub, alt)
time = 0
while n > 0:
	parity = n % 2
	(should_double, dub, alt) = worth(n)
	if should_double:
		time += dub
		if parity == 0:
			n = n // 2
			continue
		else:
			half = n // 2
			if half % 2 == 0:
				n = half
				continue
			elif (half // 2 + 1) * x + y < half * x:
				n = half + 1
				continue
			else:
				time += half * x
				break
	else:
		time += n * x
		break
print(time)