結果

問題 No.1512 作文
ユーザー LyricalMaestro
提出日時 2025-06-28 13:20:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 525 ms / 2,000 ms
コード長 908 bytes
コンパイル時間 548 ms
コンパイル使用メモリ 82,704 KB
実行使用メモリ 116,988 KB
最終ジャッジ日時 2025-06-28 13:20:19
合計ジャッジ時間 7,195 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1512

def main():
    N = int(input())
    S = []
    for _ in range(N):
        S.append(input())

    # 単体で良い文字であるかを見る
    new_S = []
    for s in S:
        pre = ""
        is_ok = True
        for i in range(len(s)):
            if pre > s[i]:
                is_ok = False
                break
            pre = s[i]
        if is_ok:
            new_S.append((ord(s[0]) - ord("a"), ord(s[-1]) - ord("a"), len(s)))
    new_S.sort()

    dp = [-1] * 26
    dp[0] = 0
    for s in new_S:
        new_dp = [-1] * 26

        for i in range(26):
            if dp[i] == -1:
                continue

            new_dp[i] = max(new_dp[i], dp[i])

            if i <= s[0]:
                new_dp[s[1]] = max(new_dp[s[1]], dp[i] + s[2])

        dp = new_dp

    print(max(dp))


    


        

    



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