結果
| 問題 | No.783 門松計画 | 
| コンテスト | |
| ユーザー |  lam6er | 
| 提出日時 | 2025-03-26 15:50:13 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,330 ms / 2,000 ms | 
| コード長 | 2,359 bytes | 
| コンパイル時間 | 284 ms | 
| コンパイル使用メモリ | 82,320 KB | 
| 実行使用メモリ | 97,808 KB | 
| 最終ジャッジ日時 | 2025-03-26 15:50:59 | 
| 合計ジャッジ時間 | 4,533 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
ソースコード
import sys
from collections import defaultdict
def main():
    N, C = map(int, sys.stdin.readline().split())
    L = list(map(int, sys.stdin.readline().split()))
    W = list(map(int, sys.stdin.readline().split()))
    
    def is_kadomatsu(a, b, c):
        if a == b or b == c or a == c:
            return False
        if (b > a and b > c) or (b < a and b < c):
            return True
        return False
    
    # Initialize DP: dp[cost] -> dictionary of (a, b, flag) : max_total
    dp = [defaultdict(int) for _ in range(C + 1)]
    dp[0][(None, None, False)] = 0
    
    for current_cost in range(C + 1):
        current_states = list(dp[current_cost].items())
        for state_info in current_states:
            (a_prev_prev, a_prev, flag), current_total = state_info
            for k in range(N):
                Lk = L[k]
                Wk = W[k]
                new_cost = current_cost + Wk
                if new_cost > C:
                    continue
                # Transition
                if a_prev_prev is None:
                    # Previous state has length 0 or 1
                    new_a_prev_prev = a_prev
                    new_a_prev = Lk
                    new_flag = flag  # No kadomatsu formed yet
                    new_state = (new_a_prev_prev, new_a_prev, new_flag)
                    new_total = current_total + Lk
                    if new_total > dp[new_cost].get(new_state, 0):
                        dp[new_cost][new_state] = new_total
                else:
                    # Check if a_prev_prev, a_prev, Lk form a kadomatsu
                    if is_kadomatsu(a_prev_prev, a_prev, Lk):
                        new_a_prev_prev = a_prev
                        new_a_prev = Lk
                        new_flag = flag or True  # Once flag is True, it remains True
                        new_state = (new_a_prev_prev, new_a_prev, new_flag)
                        new_total = current_total + Lk
                        if new_total > dp[new_cost].get(new_state, 0):
                            dp[new_cost][new_state] = new_total
    
    max_total = 0
    for cost in range(C + 1):
        for state in dp[cost]:
            a, b, flag = state
            if flag and dp[cost][state] > max_total:
                max_total = dp[cost][state]
    print(max_total)
if __name__ == "__main__":
    main()
            
            
            
        