結果

問題 No.844 split game
ユーザー lam6er
提出日時 2025-03-31 17:42:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,327 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 82,060 KB
実行使用メモリ 118,464 KB
最終ジャッジ日時 2025-03-31 17:43:41
合計ジャッジ時間 8,944 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    M = int(input[ptr])
    ptr += 1
    A = int(input[ptr])
    ptr += 1

    groups = defaultdict(list)
    for _ in range(M):
        l = int(input[ptr])
        ptr += 1
        r = int(input[ptr])
        ptr += 1
        p = int(input[ptr])
        ptr += 1
        groups[r].append((l, p))

    dp = [-float('inf')] * (N + 1)
    dp[0] = 0
    global_max = dp[0]

    for i in range(1, N + 1):
        current_max = -float('inf')

        if i in groups:
            for (l, p) in groups[i]:
                j = l - 1
                if j < 0:
                    continue  # invalid j
                if dp[j] == -float('inf'):
                    continue
                current_val = dp[j] + p
                if current_val > current_max:
                    current_max = current_val

        max_part_a = current_max if current_max != -float('inf') else -float('inf')
        max_part_b = global_max
        candidate = max(max_part_a, max_part_b)

        if i < N:
            candidate -= A

        dp[i] = candidate
        if dp[i] > global_max:
            global_max = dp[i]

    result = max(dp[N], 0)
    print(result)

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