You are playing a game. At each level of the game, you have to choose one of the roads to go to the next level. Initially, you have h amount of health and m amount of money.
If you take the first road then health decreases by 20 and money increase by 5. If you take the second road then your health decreases by 5 and money decrease by 10. If you take the third road then your health increases by 3 and money increase by 2.
You have to tell what is the maximum level you can reach up to under the constraints that in no two consecutive levels you can select the same road twice and once your health or money becomes <= 0 game ends(that level is not counted).
Example 1:
Input:
H=2
M=10
Output:
1
Explanation:
For the first level, we can only choose the third road.
Now we cannot move ahead anymore.
Example 2:
Input:
H=20
M=8
Output:
5
Explanation:
Go like this:- R3(23,10)->R1(3,15)->R3(6,17)->
R2(1,7)->R3(4,9)-> game ends as we cannot choose
any more roads.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function maxLevel() which takes in the health and the money as input and returns the maximum level that can be reached by you.
 
Expected Time Complexity: O(N*N)
Expected Auxiliary Space: O(N*N)
 
Constraints:
1 <= H,M<=800.

Difficulty: MEDIUM
Tags: Dynamic programming
Skill types: Dynamic programming
Expected Time Complexity: O(N*N)
Expected Auxiliary Space: O(N*N)

Starter code:
from typing import List





class Solution:

    def maxLevel(self, h:int,m:int) -> int:

        # code here

        



Solutions:
from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:

		def solve(h, m):
			if h <= 0 or m <= 0:
				return 0
			if dp[h][m] != -1:
				return dp[h][m]
			third_sec = solve(h - 2, m - 8) + 2
			third_first = solve(h - 17, m + 7) + 2
			dp[h][m] = max(third_sec, third_first)
			return dp[h][m]
		dp = [[-1] * 1001 for i in range(1001)]
		return solve(h, m) - 1


from typing import List
from collections import defaultdict

def multi_dict(K, type):
	if K == 1:
		return defaultdict(type)
	else:
		return defaultdict(lambda : multi_dict(K - 1, type))

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		road = 0
		res = 0
		while h > 0 and m > 0:
			if road != 1:
				h += 3
				m += 2
				res += 1
				road = 1
			elif h > 5 and m > 10 and (road != 2):
				h -= 5
				m -= 10
				res += 1
				road = 2
			elif h > 20 and road != 3:
				h -= 20
				m += 5
				res += 1
				road = 3
			else:
				break
		return res


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		dp = [[None for j in range(1500)] for i in range(1500)]

		def rec(h, m):
			if h <= 0 or m <= 0:
				return 0
			if dp[h][m] != None:
				return dp[h][m]
			a = rec(h - 5 + 3, m - 10 + 2) + 2
			b = rec(h - 20 + 3, m + 5 + 2) + 2
			dp[h][m] = max(a, b)
			return dp[h][m]
		return rec(h, m) - 1


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:

		def solve(h, m):
			if h <= 0 or m <= 0:
				return 0
			if dp[h][m] != -1:
				return dp[h][m]
			first = solve(h - 17, m + 7) + 2
			second = solve(h - 2, m - 8) + 2
			dp[h][m] = max(first, second)
			return dp[h][m]
		dp = [[-1] * 1000 for i in range(1000)]
		return solve(h, m) - 1


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:

		def solve(prev, h, m):
			if h <= 0 or m <= 0:
				return 0
			if prev == 1:
				return 1 + solve(0, h + 3, m + 2)
			else:
				if h > 5 and m > 10:
					return 1 + solve(1, h - 5, m - 10)
				elif h > 20:
					return 1 + solve(1, h - 20, m + 5)
				return 0
		res = solve(1, h, m)
		return res


from typing import List

class Solution:

	def util(self, h, m, l, r):
		if h <= 0 or m <= 0:
			return l
		l1 = l2 = l3 = 0
		if r != 'r3':
			l1 += self.util(h + 3, m + 2, l + 1, 'r3')
		if h >= 5 and m >= 10 and (r != 'r2'):
			l1 += self.util(h - 5, m - 10, l + 1, 'r2')
		if h >= 20 and r != 'r1':
			l1 += self.util(h - 20, m + 5, l + 1, 'r1')
		return l1

	def maxLevel(self, h: int, m: int) -> int:
		level = 0
		r3 = True
		while h > 0 and m > 0:
			if r3:
				h += 3
				m += 2
				r3 = False
				level += 1
			else:
				r3 = True
				level += 1
				if h > 5 and m > 10:
					h -= 5
					m -= 10
				else:
					h -= 20
					m += 5
		return level - 1


from typing import List
memo = {}

class Solution:

	def helper(self, h, m, prev):
		global memo
		if h <= 0 or m <= 0:
			return 0
		if (h, m, prev) in memo:
			return memo[h, m, prev]
		if prev == -1:
			one = 1 + self.helper(h - 20, m + 5, 1)
			two = 1 + self.helper(h - 5, m - 10, 2)
			three = 1 + self.helper(h + 3, m + 2, 3)
			memo[h, m, prev] = max(one, two, three)
			return memo[h, m, prev]
		if prev == 1:
			two = 1 + self.helper(h - 5, m - 10, 2)
			three = 1 + self.helper(h + 3, m + 2, 3)
			memo[h, m, prev] = max(two, three)
			return memo[h, m, prev]
		if prev == 2:
			one = 1 + self.helper(h - 20, m + 5, 1)
			three = 1 + self.helper(h + 3, m + 2, 3)
			memo[h, m, prev] = max(one, three)
			return memo[h, m, prev]
		if prev == 3:
			one = 1 + self.helper(h - 20, m + 5, 1)
			two = 1 + self.helper(h - 5, m - 10, 2)
			memo[h, m, prev] = max(one, two)
			return memo[h, m, prev]

	def maxLevel(self, h: int, m: int) -> int:
		d = {1: [-20, 5], 2: [-5, 10], 3: [3, 2]}
		return self.helper(h, m, -1) - 1


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		dp = {}

		def solve(h, m, b_third):
			if (h, m, b_third) in dp:
				return dp[h, m, b_third]
			if h <= 0 or m <= 0:
				return 0
			if b_third:
				a = solve(h - 20, m + 5, False)
				b = solve(h - 5, m - 10, False)
				dp[h, m, b_third] = max(a, b) + 1
				return dp[h, m, b_third]
			else:
				c = solve(h + 3, m + 2, True) + 1
				dp[h, m, b_third] = c
				return c
		return solve(h, m, False) - 1


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		memo = {}

		def recur(hh, mm, prev):
			nonlocal memo
			if mm <= 0 or hh <= 0:
				return 0
			res = 0
			if (hh, mm, prev) in memo:
				return memo[hh, mm, prev]
			if prev != 1 and hh - 20 > 0:
				res = max(res, recur(hh - 20, mm + 5, 1) + 1)
			if prev != 2 and hh - 5 > 0 and (mm - 10 > 0):
				res = max(res, recur(hh - 5, mm - 10, 2) + 1)
			if prev != 3:
				res = max(res, recur(hh + 3, mm + 2, 3) + 1)
			memo[hh, mm, prev] = res
			return res
		res = recur(h + 3, m + 2, 3) + 1
		if h - 20 > 0:
			res = max(res, recur(h - 20, m + 5, 1) + 1)
		if h - 5 > 0 and m - 10 > 0:
			res = max(res, recur(h - 5, m - 10, 2) + 1)
		return res


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		(lvl, lc) = (0, 0)
		while h > 0 or m > 0:
			if lc != 3:
				h += 3
				m += 2
				lc = 3
			elif h > 5 and m > 10 and (lc != 2):
				h -= 5
				m -= 10
				lc = 2
			elif h > 20 and lc != 1:
				h -= 20
				m += 5
				lc = 1
			else:
				break
			lvl += 1
		return lvl


from typing import List

class Solution:

	def helper(self, h, m, prev):
		if h <= 0 or m <= 0:
			return 0
		if (h, m, prev) in self.memo:
			return self.memo[h, m, prev]
		maxLevel = 0
		if prev != 0:
			maxLevel = max(maxLevel, self.helper(h - 20, m + 5, 0))
		if prev != 1:
			maxLevel = max(maxLevel, self.helper(h - 5, m - 10, 1))
		if prev != 2:
			maxLevel = max(maxLevel, self.helper(h + 3, m + 2, 2))
		self.memo[h, m, prev] = 1 + maxLevel
		return 1 + maxLevel

	def maxLevel(self, h: int, m: int) -> int:
		self.memo = {}
		return self.helper(h, m, -1) - 1

class IntArray:

	def __init__(self) -> None:
		pass

	def Input(self, n):
		arr = [int(i) for i in input().strip().split()]
		return arr

	def Print(self, arr):
		for i in arr:
			print(i, end=' ')
		print()
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		a = IntArray().Input(2)
		(h, m) = (a[0], a[1])
		obj = Solution()
		res = obj.maxLevel(h, m)
		print(res)


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		dp = [[-1 for z in range(1500)] for k in range(1500)]

		def util(a, b):
			if a <= 0 or b <= 0:
				return 0
			if dp[a][b] != -1:
				return dp[a][b]
			p = util(a + 3 - 20, b + 2 + 5) + 2
			q = util(a + 3 - 5, b + 2 - 10) + 2
			dp[a][b] = max(p, q)
			return dp[a][b]
		return util(h, m) - 1


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		count = 0
		while True:
			count = count + 1
			h = h + 3
			m = m + 2
			if h > 20 and m <= 10:
				h = h - 20
				m = m + 5
				count = count + 1
			elif h > 5 and m > 10:
				h = h - 5
				m = m - 10
				count = count + 1
			else:
				return count


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		moves = {}
		moves[1] = (-17, 7)
		moves[2] = (-2, -8)
		curr_stats = set([(h, m, -1)])
		answer = -1
		while len(curr_stats) > 0:
			answer += 2
			new_curr_stats = set()
			for stat in curr_stats:
				prev_move = stat[2]
				prev_health = stat[0]
				prev_money = stat[1]
				for move in moves:
					(change_health, change_money) = moves[move]
					new_health = prev_health + change_health
					new_money = prev_money + change_money
					if new_health > 0 and new_money > 0:
						new_curr_stats.add((new_health, new_money, move))
			curr_stats = new_curr_stats
		return answer


from typing import List

class Solution:

	def __init__(self):
		self.memo = {}

	def maxLevel(self, h: int, m: int) -> int:
		return max(self.route1(h, m, 0), self.route2(h, m, 0), self.route3(h, m, 0))

	def route1(self, h: int, m: int, count: int) -> int:
		k = (1, h, m)
		if self.memo.get(k) is None:
			self.memo[k] = max(self.route2(h + 3, m + 2, count + 1), self.route3(h + 3, m + 2, count + 1))
		return self.memo[k]

	def route2(self, h: int, m: int, count: int) -> int:
		k = (2, h, m)
		h -= 5
		m -= 10
		if h <= 0 or m <= 0:
			return count
		if self.memo.get(k) is None:
			self.memo[k] = max(self.route1(h, m, count + 1), self.route3(h, m, count + 1))
		return self.memo[k]

	def route3(self, h: int, m: int, count: int) -> int:
		k = (3, h, m)
		h -= 20
		m += 5
		if h <= 0:
			return count
		if self.memo.get(k) is None:
			self.memo[k] = max(self.route1(h, m, count + 1), self.route2(h, m, count + 1))
		return self.memo[k]


class Solution:

	def maxLevel(self, h, m):
		dp = {}

		def gameScore(h, m):
			if h <= 0 or m <= 0:
				return 0
			if (h, m) in dp:
				return dp[h, m]
			dp[h, m] = max(gameScore(h - 2, m - 8) + 2, gameScore(h - 17, m + 7) + 2)
			return dp[h, m]
		return gameScore(h, m) - 1


from typing import List
from collections import deque

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		memo = dict()

		def rec(h, m):
			if h <= 0 or m <= 0:
				return 0
			if (h, m) in memo:
				return memo[h, m]
			a = rec(h - 5 + 3, m - 10 + 2) + 2
			b = rec(h - 20 + 3, m + 5 + 2) + 2
			memo[h, m] = max(a, b)
			return max(a, b)
		return rec(h, m) - 1


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		t = []
		for i in range(h + 1):
			temp = []
			for j in range(m + 1):
				temp.append(-1)
			t.append(temp)

		def function(health, money, option):
			if t[h][m] != -1:
				return t[h][m]
			else:
				if option == 0:
					a1 = 1 + function(health + 3, money + 2, 1)
					a2 = 0
					a3 = 0
					if health > 5 and money > 10:
						a2 = 1 + function(health - 5, money - 10, 2)
					if health > 20:
						a3 = 1 + function(health - 20, money + 5, 3)
					t[h][m] = max(a1, a2, a3)
					return t[h][m]
				if option == 1:
					a2 = 0
					if health > 5 and money > 10:
						a2 = 1 + function(health - 5, money - 10, 2)
					a3 = 0
					if health > 20:
						a3 = 1 + function(health - 20, money + 5, 3)
					t[h][m] = max(a2, a3)
					return t[h][m]
				if option == 2:
					a1 = 1 + function(health + 3, money + 2, 1)
					a3 = 0
					if health > 20:
						a3 = 1 + function(health - 20, money + 5, 3)
					t[h][m] = max(a1, a3)
					return t[h][m]
				if option == 3:
					a1 = 1 + function(health + 3, money + 2, 1)
					a2 = 0
					if health > 5 and money > 10:
						a2 = 1 + function(health - 5, money - 10, 2)
					t[h][m] = max(a1, a2)
					return t[h][m]
		return function(h, m, 0)


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		end = False
		level = 0
		prevRoad = 0
		while not end:
			if prevRoad != 1:
				h += 3
				m += 2
				prevRoad = 1
				level += 1
			elif h > 5 and m > 10:
				h -= 5
				m -= 10
				prevRoad = 2
				level += 1
			elif h > 20:
				h -= 20
				m += 5
				prevRoad = 3
				level += 1
			else:
				end = True
		return level


def operation1(h, m, counts):
	counts += 1
	h -= 2
	m -= 8
	return (h, m, counts)

def operation2(h, m, counts):
	counts += 1
	h -= 17
	m += 7
	return (h, m, counts)

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		counts = 0
		while h > 2 and m > 8:
			(h, m, counts) = operation1(h, m, counts)
		while h > 17:
			if h > 17:
				(h, m, counts) = operation2(h, m, counts)
				while h > 2 and m > 8:
					(h, m, counts) = operation1(h, m, counts)
		return 2 * counts + 1


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:

		def dp(h, m, f, count):
			if h <= 0 or m <= 0:
				return count - 1
			elif f == 0:
				return dp(h + 3, m + 2, 1, count + 1)
			elif h - m > 10:
				return dp(h - 20, m + 5, 0, count + 1)
			else:
				return dp(h - 5, m - 10, 0, count + 1)
		a = dp(h, m, 0, 0)
		return a


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		d = {}

		def dp(h, m, prev, levels):
			if h <= 0 or m <= 0:
				return levels - 1
			if (h, m) in d:
				return d[h, m]
			if prev == 1:
				l = dp(h - 5, m - 10, 2, levels + 1)
				r = dp(h - 20, m + 5, 3, levels + 1)
				d[h, m] = max(l, r)
				return d[h, m]
			else:
				d[h, m] = dp(h + 3, m + 2, 1, levels + 1)
				return d[h, m]
		return dp(h, m, 2, 0)


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		dic = {}
		health = [3, -5, -20]
		money = [2, -10, 5]

		def solve(h, m, i):
			asc = h + m + i
			if h + health[i] <= 0 or m + money[i] <= 0:
				return 0
			elif asc in dic:
				return dic[asc]
			elif i == 0:
				dic[asc] = 1 + max(solve(h + health[i], m + money[i], 1), solve(h + health[i], m + money[i], 2))
				return dic[asc]
			elif i == 1:
				dic[asc] = 1 + max(solve(h + health[i], m + money[i], 0), solve(h + health[i], m + money[i], 2))
				return dic[asc]
			else:
				dic[asc] = 1 + max(solve(h + health[i], m + money[i], 0), solve(h + health[i], m + money[i], 1))
				return dic[asc]
		return max(solve(h, m, 0), solve(h, m, 1), solve(h, m, 2))


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		if m == 0:
			return 0
		level = 0
		while True:
			h += 3
			m += 2
			level += 1
			if m > 10:
				h -= 5
				m -= 10
			else:
				h -= 20
				m += 5
			if h <= 0 or m <= 0:
				break
			level += 1
		return level


from typing import List
memo = {}

class Solution:

	def maxLevel(self, h: int, m: int) -> int:

		def dfs(h, m, road):
			if h <= 0 or m <= 0:
				return 0
			toSearch = str(h) + '&' + str(m) + '&' + str(road)
			if toSearch in memo:
				return memo[toSearch]
			if road == 1:
				a = dfs(h - 5, m - 10, 2)
				b = dfs(h - 20, m + 5, 3)
				memo[toSearch] = 1 + max(a, b)
				return memo[toSearch]
			else:
				a = dfs(h + 3, m + 2, 1)
				memo[toSearch] = 1 + a
				return memo[toSearch]
		if h <= 0 or m <= 0:
			return 0
		return dfs(h + 3, m + 2, 1)


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		(h, m) = self.r1(h, m)
		l = 1
		while True:
			if min(self.r2(h, m)) < min(self.r3(h, m)) and min(self.r3(h, m)) > 0:
				(h, m) = self.r3(h, m)
				(h, m) = self.r1(h, m)
				l += 2
			elif min(self.r2(h, m)) >= min(self.r3(h, m)) and min(self.r2(h, m)) > 0:
				(h, m) = self.r2(h, m)
				(h, m) = self.r1(h, m)
				l += 2
			else:
				break
		return l

	def r1(self, h, m):
		h1 = h + 3
		m1 = m + 2
		return (h1, m1)

	def r2(self, h, m):
		h2 = h - 5
		m2 = m - 10
		return (h2, m2)

	def r3(self, h, m):
		h3 = h - 20
		m3 = m + 5
		return (h3, m3)


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		h1 = [3, 2]
		h2 = [-5, -10]
		h3 = [-20, 5]
		prev = 2
		count = 0
		while h > 0 and m > 0:
			if prev == 2:
				h += h1[0]
				m += h1[1]
				prev = 1
			elif prev == 1 and h > 5 and (m > 10):
				h += h2[0]
				m += h2[1]
				prev = 2
			elif h > 20:
				prev = 2
				h += h3[0]
				m += h3[1]
			else:
				break
			count += 1
		return count


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		(h, m) = self.R1(h, m)
		lvl = 1
		while True:
			if min(self.R2(h, m)) < min(self.R3(h, m)) and min(self.R3(h, m)) > 0:
				(h, m) = self.R3(h, m)
				(h, m) = self.R1(h, m)
				lvl += 2
			elif min(self.R2(h, m)) >= min(self.R3(h, m)) and min(self.R2(h, m)) > 0:
				(h, m) = self.R2(h, m)
				(h, m) = self.R1(h, m)
				lvl += 2
			else:
				break
		return lvl

	def R1(self, h, m):
		h1 = h + 3
		m1 = m + 2
		return (h1, m1)

	def R2(self, h, m):
		h2 = h - 5
		m2 = m - 10
		return (h2, m2)

	def R3(self, h, m):
		h3 = h - 20
		m3 = m + 5
		return (h3, m3)

class IntArray:

	def __init__(self) -> None:
		pass

	def Input(self, n):
		arr = [int(i) for i in input().strip().split()]
		return arr

	def Print(self, arr):
		for i in arr:
			print(i, end=' ')
		print()
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		a = IntArray().Input(2)
		(h, m) = (a[0], a[1])
		obj = Solution()
		res = obj.maxLevel(h, m)
		print(res)


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		r1 = [3, 2]
		r2 = [-5, -10]
		r3 = [-20, 5]
		prev = 2
		ct = 0
		while h > 0 and m > 0:
			if prev == 2:
				h += r1[0]
				m += r1[1]
				prev = 1
			elif prev == 1 and m > 10 and (h > 5):
				prev = 2
				h += r2[0]
				m += r2[1]
			elif h > 20:
				prev = 2
				h += r3[0]
				m += r3[1]
			else:
				break
			ct += 1
		return ct


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		(h, m) = self.R1(h, m)
		lvl = 1
		while True:
			if min(self.R2(h, m)) < min(self.R3(h, m)) and min(self.R3(h, m)) > 0:
				(h, m) = self.R3(h, m)
				(h, m) = self.R1(h, m)
				lvl += 2
			elif min(self.R2(h, m)) >= min(self.R3(h, m)) and min(self.R2(h, m)) > 0:
				(h, m) = self.R2(h, m)
				(h, m) = self.R1(h, m)
				lvl += 2
			else:
				break
		return lvl

	def R1(self, h, m):
		h1 = h + 3
		m1 = m + 2
		return (h1, m1)

	def R2(self, h, m):
		h2 = h - 5
		m2 = m - 10
		return (h2, m2)

	def R3(self, h, m):
		h3 = h - 20
		m3 = m + 5
		return (h3, m3)


from typing import List
from functools import lru_cache

class Solution:

	def maxLevel(self, h: int, m: int) -> int:

		@lru_cache(None)
		def dp(H, M):
			if H <= 0 or M <= 0:
				return -1
			res = 0
			if H > 5 and M > 10:
				res = 2 + dp(H - 5 + 3, M - 10 + 2)
			if H > 20:
				res = max(res, 2 + dp(H - 20 + 3, M + 5 + 2))
			return res
		return 1 + dp(h + 3, m + 2)


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		(f1, c) = (0, 0)
		while h > 0 and m > 0:
			if f1 != 1:
				h += 3
				m += 2
				f1 = 1
			elif m > 10 and h > 5:
				f1 = 0
				h -= 5
				m -= 10
			elif h > 20:
				f1 = 0
				h -= 20
				m += 5
			else:
				break
			c += 1
		return c


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		ans = 0
		p = True
		while True:
			if p:
				h += 3
				m += 2
				ans += 1
				p = False
			elif h > 5 and m > 10:
				h -= 5
				m -= 10
				ans += 1
				p = True
			elif h > 20:
				h -= 20
				m += 5
				ans += 1
				p = True
			else:
				break
		return ans


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		level = 0
		cur = 0
		while h > 0 and m > 0:
			if not cur:
				level += 1
				h += 3
				m += 2
				cur = 1
			if cur and h > 5 and (m > 10):
				level += 1
				h -= 5
				m -= 10
				cur = 0
			elif cur and h > 20:
				level += 1
				h -= 20
				m += 5
				cur = 0
			else:
				break
		return level


from typing import List
import math

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		cost = [[3, 2], [-5, -10], [-20, 5]]
		score = [h, m]
		count = 0
		i = -1
		while score[0] > 0 and score[1] > 0:
			maxi = -math.inf
			index = None
			for k in range(3):
				if k == i:
					continue
				new = [score[0] + cost[k][0], score[1] + cost[k][1]]
				if new[0] <= 0 or new[1] <= 0:
					continue
				if sum(new) > maxi:
					maxi = sum(new)
					index = k
			if index == None:
				break
			i = index
			score = [score[0] + cost[i][0], score[1] + cost[i][1]]
			count += 1
		return count


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		cache = {}

		def dp(h, m):
			if h <= 0 or m <= 0:
				return -1
			if (h, m) in cache:
				return cache[h, m]
			cache[h, m] = max(dp(h + 3 - 5, m + 2 - 10), dp(h + 3 - 20, m + 2 + 5)) + 2
			return cache[h, m]
		return dp(h, m)


from typing import List
ans = [[0 for i in range(5000)] for j in range(5000)]

class Solution:

	def maxLevel(self, h: int, m: int) -> int:

		def game(heal, mon, lvl):
			if heal <= 0 or mon <= 0:
				return 1
			(heal, mon) = (heal + 3, mon + 2)
			if ans[heal][mon]:
				return ans[heal][mon] - 2
			mx1 = game(heal - 5, mon - 10, lvl + 2)
			mx2 = game(heal - 20, mon + 5, lvl + 2)
			ans[heal][mon] = 2 + max(mx1, mx2)
			return ans[heal][mon]
		return max(game(h, m, 0) - 2, 0)


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		count = 0

		def road1(h, m):
			l = [h + 3, m + 2]
			return l

		def road2(h, m):
			l = [h - 5, m - 10]
			return l

		def road3(h, m):
			l = [h - 20, m + 5]
			return l
		r = 0
		r = 0
		while h > 0 and m > 0:
			if r != 1:
				l = road1(h, m)
				h = l[0]
				m = l[1]
				r = 1
			elif (h > 5 and m > 10) and r != 2:
				l1 = road2(h, m)
				h = l1[0]
				m = l1[1]
				r = 2
			elif h > 20 and r != 3:
				l2 = road3(h, m)
				h = l2[0]
				m = l2[1]
				r = 3
			else:
				break
			count += 1
		return count


from typing import List
from functools import lru_cache
import sys
sys.setrecursionlimit(2000)

class Solution:

	def maxLevel(self, h: int, m: int) -> int:

		@lru_cache(maxsize=None)
		def recSol(h, m):
			bla = 0
			if h <= 0 or m <= 0:
				return bla
			bla = max(recSol(h - 2, m - 8), recSol(h - 17, m + 7), 0)
			return bla + 1
		val = recSol(h, m)
		if val == 0:
			return 0
		return val * 2 - 1


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		j = k = i = 0
		lev = 0
		while h > 0 and m > 0:
			if k < 1:
				h = h + 3
				m = m + 2
				k += 1
				i = j = 0
			elif h > 5 and m > 10 and (j < 1):
				h = h - 5
				m = m - 10
				j += 1
				i = k = 0
			elif h > 20 and i < 1:
				h = h - 20
				m = m + 5
				i += 1
				j = k = 0
			else:
				break
			lev += 1
		return lev


from typing import List

class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		H = h
		M = m
		level = 0
		while H > 0 and M > 0:
			if level % 2 == 0:
				H += 3
				M += 2
			elif H - 5 > 0 and M - 10 > 0:
				H -= 5
				M -= 10
			elif H - 20 > 0:
				H -= 20
				M += 5
			else:
				return level
			level += 1


from typing import List
result = [[0] * 2001 for _ in range(2002)]

class Solution:

	def maxLevel(self, h: int, m: int) -> int:

		def solve(health, money, level):
			global result
			if health <= 0 or money <= 0:
				return 1
			health += 3
			money += 2
			if result[health][money]:
				return result[health][money] - 2
			rslt1 = solve(health - 5, money - 10, level + 2)
			rslt2 = solve(health - 20, money + 5, level + 2)
			result[health][money] = max(rslt1, rslt2) + 2
			return result[health][money]
		return max(0, solve(h, m, 0) - 2)


class Solution:

	def maxLevel(self, h: int, m: int) -> int:
		level = 0
		r1 = False
		while h > 0 and m > 0:
			level += 1
			if not r1:
				h += 3
				m += 2
				r1 = True
			else:
				(h2, m2, h3, m3) = (h - 5, m - 10, h - 20, m + 5)
				if h2 > 0 and m2 > 0:
					(h, m) = (h2, m2)
				else:
					(h, m) = (h3, m3)
				r1 = False
		return level - 1