結果
問題 | No.783 門松計画 |
ユーザー |
![]() |
提出日時 | 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 sysfrom collections import defaultdictdef 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 Falseif (b > a and b > c) or (b < a and b < c):return Truereturn False# Initialize DP: dp[cost] -> dictionary of (a, b, flag) : max_totaldp = [defaultdict(int) for _ in range(C + 1)]dp[0][(None, None, False)] = 0for 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_infofor k in range(N):Lk = L[k]Wk = W[k]new_cost = current_cost + Wkif new_cost > C:continue# Transitionif a_prev_prev is None:# Previous state has length 0 or 1new_a_prev_prev = a_prevnew_a_prev = Lknew_flag = flag # No kadomatsu formed yetnew_state = (new_a_prev_prev, new_a_prev, new_flag)new_total = current_total + Lkif new_total > dp[new_cost].get(new_state, 0):dp[new_cost][new_state] = new_totalelse:# Check if a_prev_prev, a_prev, Lk form a kadomatsuif is_kadomatsu(a_prev_prev, a_prev, Lk):new_a_prev_prev = a_prevnew_a_prev = Lknew_flag = flag or True # Once flag is True, it remains Truenew_state = (new_a_prev_prev, new_a_prev, new_flag)new_total = current_total + Lkif new_total > dp[new_cost].get(new_state, 0):dp[new_cost][new_state] = new_totalmax_total = 0for cost in range(C + 1):for state in dp[cost]:a, b, flag = stateif flag and dp[cost][state] > max_total:max_total = dp[cost][state]print(max_total)if __name__ == "__main__":main()