ABC231 E - Minimal payments
https://atcoder.jp/contests/abc231/tasks/abc231_e (opens in a new tab)
้่ฒไธไฝใDPใ
ๅ็กฌ่ฒจ ใซใคใใฆใๆฏๆใ้้กใ่ถ ใใชใใใใซ ๆๆใใใใ้ฃใใใใใฃใฆ ๆๆใใใฎ 2 ้ใๅญๅจใใใ ใใใใฎๆฏๆใใซใใใฆใ ไปฅไธใฎ็กฌ่ฒจใไฝฟใฃใฆๆฏๆใในใ้้กใฏใใใใ ๅใ ๅใจใชใใ
ๅใงใไธใจๅๆงใซ่ถ ใใ๏ผ่ถ ใใชใใใใซๆฏๆใใจใ ไปฅไธใฎ็กฌ่ฒจใไฝฟใฃใฆๆฏๆใในใ้้กใฏใ ๅใ ๅใ ๅใ ๅใจใชใใ
ใใใงใ ใงใใใใจใ่ธใพใใใจใไธใฎๅ้้กใฏ็ตๅฑ ๅใ ใฎ 2 ็จฎ้กใงใใใ
ใใฃใฆใๅ็กฌ่ฒจใงๆฏๆใ้ใซใใฎ้้กไปฅไธใฎ็กฌ่ฒจใงๆฏๆใในใใใใช้้กใจใใใฎใฏใ้ซใ 2 ็จฎ้กใซ่ฝใก็ใใฎใงใ้้กใฎๅคงใใใณใคใณใใ้ ็ชใซใ่ถ ใใชใใใใซๆฏๆใใใใ้ฃใใใใใใใใซๆฏๆใใใฎ 2 ้ใใ่ใใฆใใใฐ่ฏใใ
def memorize(f):
memo = {}
def inner(*args):
if args not in memo:
memo[args] = f(*args)
return memo[args]
return inner
@memorize
def rec(i, x):
if i == 0:
return x
return min(x // a[i] + rec(i - 1, x % a[i]),
x // a[i] + 1 + rec(i - 1, -x % a[i]))
n, x = map(int, input().split())
a = list(map(int, input().split()))
print(rec(n - 1, x))