1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| ''' Author: NEFU AB-IN Date: 2024-06-27 11:23:00 FilePath: \LeetCode\2786\2786.py LastEditTime: 2024-06-27 12:39:12 '''
from functools import cache from sys import setrecursionlimit, stdin, stdout, exit from collections import Counter, deque, defaultdict from heapq import heapify, heappop, heappush, nlargest, nsmallest from bisect import bisect_left, bisect_right from datetime import datetime, timedelta from string import ascii_lowercase, ascii_uppercase from math import log, gcd, sqrt, fabs, ceil, floor from typing import TypeVar, List, Dict, Any, Union, Generic
TYPE = TypeVar('TYPE')
class SA(Generic[TYPE]): def __init__(self, x: TYPE, y: TYPE): self.x: TYPE = x self.y: TYPE = y
def __lt__(self, other: 'SA[TYPE]') -> bool: return self.x < other.x def __eq__(self, other: 'SA[TYPE]') -> bool: return self.x == other.x and self.y == other.y def __repr__(self) -> str: return f'SA(x={self.x}, y={self.y})'
N = int(2e5 + 10) M = int(20) INF = int(2e9) OFFSET = int(100)
setrecursionlimit(INF)
input = lambda: stdin.readline().rstrip("\r\n") read = lambda: map(int, input().split()) read_list = lambda: list(map(int, input().split()))
class std: letter_to_num = staticmethod(lambda x: ord(x.upper()) - 65) num_to_letter = staticmethod(lambda x: ascii_uppercase[x]) array = staticmethod(lambda x=0, size=N: [x] * size) array2d = staticmethod(lambda x=0, rows=N, cols=M: [std.array(x, cols) for _ in range(rows)]) max = staticmethod(lambda a, b: a if a > b else b) min = staticmethod(lambda a, b: a if a < b else b) filter = staticmethod(lambda func, iterable: list(filter(func, iterable))) @staticmethod def find(container: Union[List[TYPE], str], value: TYPE) -> int: """Returns the index of value in container or -1 if value is not found.""" if isinstance(container, list): try: return container.index(value) except ValueError: return -1 elif isinstance(container, str): return container.find(value)
class Solution: def maxScore(self, nums: List[int], x: int) -> int: n = len(nums) dp = std.array(-INF, n) dp[0] = nums[0] res = nums[0] if nums[0] % 2 == 0: even_mx, odd_mx = nums[0], -INF else: even_mx, odd_mx = -INF, nums[0] for i in range(1, n): if nums[i] % 2 == 0: dp[i] = std.max(dp[i], even_mx + nums[i]) dp[i] = std.max(dp[i], odd_mx + nums[i] - x) even_mx = std.max(even_mx, dp[i]) if nums[i] % 2 != 0: dp[i] = std.max(dp[i], odd_mx + nums[i]) dp[i] = std.max(dp[i], even_mx + nums[i] - x) odd_mx = std.max(odd_mx, dp[i])
res = std.max(res, dp[i]) return res
print(Solution().maxScore([85,12], 79))
|