結果

問題 No.1194 Replace
ユーザー gew1fw
提出日時 2025-06-12 20:40:01
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 973 bytes
コンパイル時間 327 ms
コンパイル使用メモリ 81,932 KB
実行使用メモリ 132,364 KB
最終ジャッジ日時 2025-06-12 20:40:19
合計ジャッジ時間 6,279 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    idx = 0
    N = int(data[idx])
    idx += 1
    M = int(data[idx])
    idx +=1
    
    ops_dict = {}
    for _ in range(M):
        B_i = int(data[idx])
        idx +=1
        C_i = int(data[idx])
        idx +=1
        if C_i > B_i:
            if B_i in ops_dict:
                if C_i > ops_dict[B_i]:
                    ops_dict[B_i] = C_i
            else:
                ops_dict[B_i] = C_i
    
    sorted_B = sorted(ops_dict.keys(), reverse=True)
    dp = {}
    for x in sorted_B:
        dp[x] = x
    
    for x in sorted_B:
        c = ops_dict[x]
        if c in dp:
            new_val = max(x, dp[c])
        else:
            new_val = max(x, c)
        if new_val > dp[x]:
            dp[x] = new_val
    
    initial_sum = N * (N + 1) // 2
    for x in sorted_B:
        initial_sum += (dp[x] - x)
    
    print(initial_sum)

if __name__ == "__main__":
    main()
0