= map(int,input().split()) A2, B2 = map(int,input().split()) A3, B3 = map(int,input().split()) if A2 * B1 < A1 * B2: A1, B1, A2, B2 = A2, B2, A1, B1 if A3 * B1 < A1 * B3: A1, B1, A3, B3 = A3, B3, A1, B1 c1min = max(N // A1 - A2 - A3, 0) dplen = N - c1min * A1 r = 0 dp = [0] * (dplen + 1) for i in range(dplen + 1): dp[i] = r = max( dp[i - A1] + B1 if i >= A1 else 0, dp[i - A2] + B2 if i >= A2 else 0, dp[i - A3] + B3 if i >= A3 else 0, r, ) print(r + c1min * B1) を全探索 (Python3/PyPy3) N = int(input()) A1, B1 = map(int,input().split()) A2, B2 = map(int,input().split()) A3, B3 = map(int,input().split()) if A2 * B1 < A1 * B2: A1, B1, A2, B2 = A2, B2, A1, B1 if A3 * B1 < A1 * B3: A1, B1, A3, B3 = A3, B3, A1, B1 print( max( t // A1 * B1 + p + q for p, s in zip(range(0, A1 * B2, B2), range(N, -1, -A2)) for q, t in zip(range(0, A1 * B3, B3), range(s, -1, -A3)) ) ) コード例 F. Udon Coupon (Hard) (PyPy3) yukicoder contest 398 解説 2023-07-21 VRC競プロ部 21