#
# Created on Thu Jan 06 2022
#
# The MIT License (MIT)
# Copyright (c) 2022 Maatuq
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
from functools import lru_cache
from typing import List
from bisect import bisect_right


class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        @lru_cache(maxsize=None)
        def dp(i):
            if i >= len(days):
                return 0
            one_pass = costs[0] + dp(i + 1)
            next_seven_i = bisect_right(days, days[i] + 7 - 1)
            seven_pass = costs[1] + dp(next_seven_i)
            next_thiry_i = bisect_right(days, days[i] + 30 - 1)
            thiry_pass = costs[2] + dp(next_thiry_i)
            return min(one_pass, seven_pass, thiry_pass)

        return dp(0)


solver = Solution()
days = [1, 4, 6, 7, 8, 20]
costs = [2, 7, 15]
days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31]
costs = [2, 7, 15]
print(solver.mincostTickets(days, costs))
