結果
| 問題 | No.2423 Merge Stones | 
| コンテスト | |
| ユーザー |  gew1fw | 
| 提出日時 | 2025-06-12 19:55:01 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 2,072 bytes | 
| コンパイル時間 | 218 ms | 
| コンパイル使用メモリ | 81,832 KB | 
| 実行使用メモリ | 65,020 KB | 
| 最終ジャッジ日時 | 2025-06-12 19:55:50 | 
| 合計ジャッジ時間 | 5,611 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 27 WA * 45 | 
ソースコード
def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    
    idx = 0
    N = int(data[idx])
    idx += 1
    K = int(data[idx])
    idx += 1
    
    A = list(map(int, data[idx:idx+N]))
    idx += N
    C = list(map(int, data[idx:idx+N]))
    idx += N
    
    C_extended = C + C
    A_extended = A + A
    
    # 预处理颜色之间的连通性
    colors = set(C)
    if not colors:
        print(0)
        return
    
    class DSU:
        def __init__(self, elements):
            self.parent = {}
            for e in elements:
                self.parent[e] = e
        
        def find(self, x):
            if self.parent[x] != x:
                self.parent[x] = self.find(self.parent[x])
            return self.parent[x]
        
        def union(self, x, y):
            fx = self.find(x)
            fy = self.find(y)
            if fx != fy:
                self.parent[fy] = fx
    
    dsu = DSU(colors)
    color_list = list(colors)
    for i in range(len(color_list)):
        for j in range(i+1, len(color_list)):
            c1 = color_list[i]
            c2 = color_list[j]
            if abs(c1 - c2) <= K:
                dsu.union(c1, c2)
    
    C_extended_processed = [dsu.find(c) for c in C_extended]
    
    # 预计算每个石子的根颜色
    roots = [dsu.find(c) for c in C]
    # 检查整个圆周是否连通
    if len(set(roots)) == 1:
        print(sum(A))
        return
    
    max_total = 0
    # 遍历所有可能的区间
    for i in range(N):
        current_set = set()
        current_sum = 0
        for m in range(1, N+1):
            j = i + m - 1
            root = C_extended_processed[j]
            if m == 1:
                current_set = {root}
                current_sum = A_extended[j]
            else:
                current_set.add(root)
                current_sum += A_extended[j]
            if len(current_set) == 1:
                if current_sum > max_total:
                    max_total = current_sum
    print(max_total)
if __name__ == "__main__":
    main()
            
            
            
        