結果
問題 |
No.783 門松計画
|
ユーザー |
![]() |
提出日時 | 2025-03-31 17:36:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 289 ms / 2,000 ms |
コード長 | 1,887 bytes |
コンパイル時間 | 195 ms |
コンパイル使用メモリ | 82,588 KB |
実行使用メモリ | 78,104 KB |
最終ジャッジ日時 | 2025-03-31 17:36:58 |
合計ジャッジ時間 | 3,081 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 |
ソースコード
def main(): import sys input = sys.stdin.read().split() idx = 0 N, C = int(input[idx]), int(input[idx + 1]) idx += 2 L = list(map(int, input[idx:idx + N])) idx += N W = list(map(int, input[idx:idx + N])) INF = float('-inf') # Initialize DP: dp[i][j][c] = max sum with last two elements i, j and cost c dp = [[[INF] * (C + 1) for _ in range(N)] for __ in range(N)] for i in range(N): for j in range(N): if i == j: continue cost_initial = W[i] + W[j] if cost_initial <= C: sum_initial = L[i] + L[j] if sum_initial > dp[i][j][cost_initial]: dp[i][j][cost_initial] = sum_initial max_answer = 0 for current_cost in range(C + 1): for i in range(N): for j in range(N): if i == j: continue if dp[i][j][current_cost] == INF: continue for k in range(N): a = L[i] b = L[j] c = L[k] # Check all distinct if a == b or b == c or a == c: continue # Check if b is max or min in the triplet if not ((b > a and b > c) or (b < a and b < c)): continue new_cost = current_cost + W[k] if new_cost > C: continue new_sum = dp[i][j][current_cost] + c if new_sum > max_answer: max_answer = new_sum # Update the DP state if new_sum > dp[j][k][new_cost]: dp[j][k][new_cost] = new_sum print(max_answer) if __name__ == '__main__': main()