The finalists of the "Russian Code Cup" competition in 2214 will be the participants who win in one of the elimination rounds.

The elimination rounds are divided into main and additional. Each of the main elimination rounds consists of c problems, the winners of the round are the first n people in the rating list. Each of the additional elimination rounds consists of d problems. The winner of the additional round is one person. Besides, k winners of the past finals are invited to the finals without elimination.

As a result of all elimination rounds at least n·m people should go to the finals. You need to organize elimination rounds in such a way, that at least n·m people go to the finals, and the total amount of used problems in all rounds is as small as possible.


-----Input-----

The first line contains two integers c and d (1 ≤ c, d ≤ 100) — the number of problems in the main and additional rounds, correspondingly. The second line contains two integers n and m (1 ≤ n, m ≤ 100). Finally, the third line contains an integer k (1 ≤ k ≤ 100) — the number of the pre-chosen winners. 


-----Output-----

In the first line, print a single integer — the minimum number of problems the jury needs to prepare.


-----Examples-----
Input
1 10
7 2
1

Output
2

Input
2 2
2 1
2

Output
0

Difficulty: MEDIUM
Tags: Mathematics

Dynamic programming

Implementation
Skill types: Dynamic programming

Starter code:

Solutions:
(c, d) = list(map(int, input().split(' ')))
(n, m) = list(map(int, input().split(' ')))
k = int(input())
dp = [0] * 100000
need = n * m - k
if need <= 0:
	print(0)
	quit()
for i in range(1, 100000):
	dp[i] = min(dp[i - n] + c, dp[i - 1] + d)
print(dp[need])


(c, d) = [int(x) for x in input().strip().split()]
(n, m) = [int(x) for x in input().strip().split()]
k = int(input().strip())
t = n * m - k
if t <= 0:
	print(0)
else:
	dp = [0] * (t + 1)
	for i in range(1, t + 1):
		dp[i] = min(dp[i - 1] + d, c + dp[max(0, i - n)])
	print(dp[t])


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
x = n * m
if k >= x:
	print(0)
	return
if c <= d:
	(div, mod) = ((x - k) // n, (x - k) % n)
	if mod > 0:
		div += 1
	print(div * c)
elif d * n <= c:
	print((x - k) * d)
else:
	res = (x - k) // n * c
	mod = (x - k) % n
	if c < mod * d:
		res += c
	else:
		res += mod * d
	print(res)


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
L = 10001
for f in range(m + 1):
	for g in range(n * m + 1):
		if f * n + g + k >= n * m:
			L = min(L, f * c + g * d)
print(L)


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
dp = [0] * ((n + 1) * m + k)
for i in range(1, n * m - k + 1):
	dp[i] = min(dp[i - 1] + d, dp[i - n] + c)
print(dp[n * m - k])


(Pm, Pa) = map(int, input().split())
(N, M) = map(int, input().split())
k = int(input())
Rm = Pm / N
Ra = Pa
(lo, hi) = (0, 100000)

def count(mid):
	global Pm, Pa, Rm, Ra, N
	ans = 0
	while True:
		if mid < min(Pm, Pa):
			break
		if Ra <= Rm:
			if mid >= Pa:
				ans += 1
				mid -= Pa
			else:
				ans += N
				mid -= Pm
		elif mid >= Pm:
			ans += N
			mid -= Pm
		else:
			ans += 1
			mid -= Pa
	return ans
while lo < hi:
	mid = lo + hi >> 1
	if count(mid) >= N * M - k:
		hi = mid
	else:
		lo = mid + 1
print(lo)


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
x = n * m
from sys import exit
if k >= x:
	print(0)
	exit()
if c <= d:
	(div, mod) = ((x - k) // n, (x - k) % n)
	if mod > 0:
		div += 1
	print(div * c)
elif d * n <= c:
	print((x - k) * d)
else:
	res = (x - k) // n * c
	mod = (x - k) % n
	if c < mod * d:
		res += c
	else:
		res += mod * d
	print(res)


(c, d) = list(map(int, input().split()))
(n, m) = list(map(int, input().split()))
k = int(input())
kolvo = m * n - k
if kolvo < 0:
	print(0)
	exit()
print(min(c * (kolvo // n), d * n * (kolvo // n)) + min(c, d * (kolvo % n)))


def readln():
	return tuple(map(int, input().split()))
(c, d) = readln()
(n, m) = readln()
(k,) = readln()
ans = 1 << 30
for x in range(10001):
	y = max(0, n * m - k - n * x)
	if y >= 0 and ans > c * x + d * y:
		ans = c * x + d * y
print(ans)


import sys
(c, d) = map(int, sys.stdin.readline().split())
(n, m) = map(int, sys.stdin.readline().split())
k = int(sys.stdin.readline())
total = n * m - k
if total <= 0:
	print(0)
	return
a = [d * i for i in range(0, total + 1)]
for i in range(1, total + 1):
	if i <= n:
		if a[i] > c:
			a[i] = c
	elif a[i - n] + c < a[i]:
		a[i] = a[i - n] + c
print(a[total])


(c, d) = list(map(int, input().split()))
(n, m) = list(map(int, input().split()))
k = int(input())
S = 0
if n * m > k:
	nub = n * m - k
	S = 100000000000
	S0 = S
	i = 0
	while S0 <= S and i <= int(nub / n + 2):
		S = S0
		S0 = i * c + d * max(0, nub - n * i)
		i += 1
print(S)


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
z = m * n - k
if z <= 0:
	print(0)
else:
	dp = [0] * 100000
	for i in range(z):
		dp[i + 1] = min(dp[i] + d, dp[i + 1 - n] + c)
	print(dp[z])


(c, d) = list(map(int, input().split()))
(n, m) = list(map(int, input().split()))
k = int(input())
t = max(m * n - k, 0)
p = min(c, n * d)
ans = p * (t // n)
rem = t % n
ans = ans + min(c, rem * d)
print(ans)


(c, d) = list(map(int, input().split()))
(n, m) = list(map(int, input().split()))
k = int(input())
total = n * m - k
if k >= n * m:
	print(0)
elif c < d:
	if total % n == 0:
		print(total // n * c)
	else:
		print((total // n + 1) * c)
else:
	nr = 0
	nn = c / n
	nm = d
	if nn < nm:
		nr = total // n
		npn = nr * c
		if nr * n < total:
			npn = (nr + 1) * c
		npm = nr * c + d * (total - nr * n)
		print(min(npn, npm))
	else:
		print(d * total)


def f(x, y):
	return x * c + y * d
(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = n * m - int(input())
(x, s) = (0, max(0, k * d))
while k > x * n:
	t = f(x, k - x * n)
	if t < s:
		s = t
	x += 1
print(min(s, f(x, 0)))


import math as mt
import sys, string
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
(c, d) = M()
(n, m) = M()
k = I()
if n * m <= k:
	print(0)
else:
	t = n * m - k
	if n / c < 1 / d:
		print(d * t)
	else:
		q = t // n
		p = t % n
		print(min(q * c + c, q * c + p * d))


(c, d) = list(map(int, input().split()))
(n, m) = list(map(int, input().split()))
k = int(input())
s = max(n * m - k, 0)
res = 0
while s:
	if c > min(s, n) * d:
		res += min(s, n) * d
	else:
		res += c
	s -= min(s, n)
print(res)


([c, d], [n, m], k) = (list(map(int, input().split())), list(map(int, input().split())), int(input()))
left = n * m - k
if left <= 0:
	print(0)
else:
	print(min(left // n * c + left % n * d, (left + n - 1) // n * c, left * d))


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
t = n * m - k
if t <= 0:
	print(0)
elif d <= c / n:
	print(d * t)
elif t % n == 0:
	print(c * (t // n))
else:
	a1 = c * (t // n + 1)
	a2 = c * (t // n) + d * (t % n)
	print(min(a1, a2))


import math
(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
if m * n <= k:
	print(0)
else:
	c1 = m * c
	c2 = (m * n - k) // n * c + (m * n - k) % n * d
	c3 = math.ceil((m * n - k) / n) * c
	c4 = (m * n - k) * d
	print(min(c1, c2, c3, c4))


import math
(c, d) = list(map(int, input('').split()))
(n, m) = list(map(int, input('').split()))
k = int(input(''))
if n * m - k < 0:
	print('0')
	return
if 1 / d > n / c:
	print((n * m - k) * d)
elif math.ceil((n * m - k) / n) * c < (n * m - k) // n * c + (n * m - k) % n * d:
	print(math.ceil((n * m - k) / n) * c)
else:
	print((n * m - k) // n * c + (n * m - k) % n * d)


(c, d) = list(map(int, input().split(' ')))
(n, m) = list(map(int, input().split(' ')))
k = int(input())
mini = n * m - k
if mini < 1:
	print(0)
elif d * n <= c:
	print(d * mini)
else:
	total = mini // n * c
	mini = mini % n
	total += min(c, d * mini)
	print(total)


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
f = [float('+inf') for i in range(n * m + n + k + 2)]
f[0] = f[k] = 0
for i in range(n * m):
	f[i + 1] = min(f[i + 1], f[i] + d)
	f[i + n] = min(f[i + n], f[i] + c)
print(min(f[n * m:]))


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
tot = n * m
if k >= tot:
	print('0')
else:
	rem = tot - k
	x = n / c
	y = 1 / d
	ans = 0
	if x >= y:
		temp = rem // n
		bacha = rem % n
		if bacha * d <= c:
			ans = temp * c + bacha * d
		else:
			ans = (temp + 1) * c
	else:
		ans = rem * d
	print(ans)


(c, d) = list(map(int, input().split()))
(n, m) = list(map(int, input().split()))
f = n * m
k = int(input())
result = 0
uch = k
useosn = usedop = 0
while uch < f:
	if f - uch >= n:
		if c / n < d:
			result += c
			uch += n
		else:
			result += d
			uch += 1
	elif c > d * (f - uch):
		while uch < f:
			result += d
			uch += 1
	else:
		result += c
		uch += n
print(max(0, result))


(c, d) = [int(i) for i in input().split(' ')]
(n, m) = [int(i) for i in input().split(' ')]
k = int(input())
s = n * m - k
s = max(s, 0)
if c < d * n:
	stuff = s // n
	try1 = c * stuff + d * (s - n * stuff)
	try2 = c * (stuff + 1)
	print(min(try1, try2))
else:
	print(d * s)


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
p = 0
k = int(input())
if m * n <= k:
	print(0)
else:
	a = [0] * (m * n + 1)
	for i in range(k + 1, m * n + 1):
		a[i] = min(a[i - n] + c, a[i - 1] + d)
	print(a[m * n])


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
var = n * m - k
dp = [0] * (n * m + 1)
for i in range(1, n * m + 1):
	if i < n:
		dp[i] = dp[i - 1] + d
	else:
		dp[i] = min(dp[i - n] + c, dp[i - 1] + d)
print(min(dp[max(0, n * m - k):n * m + 1]))


def roof(n, p):
	r = p // n
	if p % n > 0:
		r = r + 1
	return r
(c, d) = (int(element) for element in input().split())
(n, m) = (int(element) for element in input().split())
k = int(input())
needed_number_of_problems = 0
p = m * n - k
while p > 0:
	x = roof(n, p)
	if d * p < c * x:
		needed_number_of_problems += d
		p = p - 1
	else:
		needed_number_of_problems += c
		p = p - n
print(needed_number_of_problems)


import math
(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
if k >= n * m:
	print('0')
else:
	left = n * m - k
	t = c * math.ceil(left / n)
	j = c * (left // n) + left % n * d
	l = d * left
	print(min(t, j, l))


problems = input().split()
c = int(problems[0])
d = int(problems[1])
n_m = input().split()
n = int(n_m[0])
m = int(n_m[1])
k = int(input())
t = n * m - k
ans = 100000000
i = 0
while i * n <= t:
	ans = min(ans, (t - i * n) * d + i * c)
	i += 1
ans = min(ans, i * c)
print(ans)


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
z = 0
best = 10 ** 10
while 1:
	x = n * m - k
	x -= z * n
	best = min(best, z * c + max(x, 0) * d)
	if x < 0:
		break
	z += 1
print(best)


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
need = n * m - k
if need <= 0:
	print(0)
else:
	r1 = c / n
	if r1 < d:
		(div, rest) = divmod(need, n)
		cont = div * c
		cont += min(c, d * rest)
		print(max(0, cont))
	else:
		print(need * d)


(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
r = n * m - k
if r <= 0:
	print(0)
	exit()
cnt = 0
x = min(d * n, c)
while r >= n:
	r -= n
	cnt += x
if r == 0:
	print(cnt)
	exit()
y = min(r * d, c)
print(cnt + y)


from math import ceil, floor
(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
Needed = n * m
if Needed <= k:
	print(0)
	exit()
Needed -= k
x = ceil(Needed / n) * c
y = ceil(Needed) * d
z = floor(Needed / n) * c + Needed % n * d
print(min(x, y, z))


bot = True
(c, d) = list(map(int, input().split()))
(n, m) = list(map(int, input().split()))
k = int(input())
kolvo = m * n - k
if kolvo < 0:
	print(0)
	return
print(min(c * (kolvo // n), d * n * (kolvo // n)) + min(c, d * (kolvo % n)))


import math
(c, d) = map(int, input().split())
(n, m) = map(int, input().split())
k = int(input())
if k >= n * m:
	print(0)
	exit()
left = n * m - k
print(min(math.ceil(left / n) * c, left // n * c + left % n * d, left * d))