結果
問題 |
No.2423 Merge Stones
|
ユーザー |
![]() |
提出日時 | 2025-06-12 18:37:37 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,700 bytes |
コンパイル時間 | 212 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 64,256 KB |
最終ジャッジ日時 | 2025-06-12 18:37:55 |
合計ジャッジ時間 | 6,097 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 27 WA * 45 |
ソースコード
import sys from collections import deque from itertools import chain def main(): N, K = map(int, sys.stdin.readline().split()) A = list(map(int, sys.stdin.readline().split())) C = list(map(int, sys.stdin.readline().split())) unique_colors = list(set(C)) color_adj = {} for color in unique_colors: color_adj[color] = [] # Build adjacency list for colors for u in unique_colors: for v in unique_colors: if abs(u - v) <= K: color_adj[u].append(v) # Find connected components using BFS visited = set() components = [] for color in unique_colors: if color not in visited: q = deque() q.append(color) visited.add(color) component = {color} while q: current = q.popleft() for neighbor in color_adj[current]: if neighbor in unique_colors and neighbor not in visited: visited.add(neighbor) component.add(neighbor) q.append(neighbor) components.append(component) max_total = 0 for component in components: current_max = 0 for i in range(N): current_sum = 0 for length in range(N): j = (i + length) % N if C[j] not in component: break current_sum += A[j] if current_sum > current_max: current_max = current_sum if current_max > max_total: max_total = current_max print(max_total) if __name__ == "__main__": main()