結果

問題 No.518 ローマ数字の和
ユーザー lam6er
提出日時 2025-03-20 18:52:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 934 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 82,660 KB
実行使用メモリ 54,216 KB
最終ジャッジ日時 2025-03-20 18:53:04
合計ジャッジ時間 1,831 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

def roman_to_int(s):
    roman = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    total = 0
    prev_value = 0
    for c in reversed(s):
        current_value = roman[c]
        if current_value < prev_value:
            total -= current_value
        else:
            total += current_value
        prev_value = current_value
    return total

def int_to_roman(num):
    val = [
        1000, 900, 500, 400,
        100, 90, 50, 40,
        10, 9, 5, 4, 1
    ]
    syms = [
        "M", "CM", "D", "CD",
        "C", "XC", "L", "XL",
        "X", "IX", "V", "IV",
        "I"
    ]
    res = ''
    i = 0
    while num > 0:
        while num >= val[i]:
            res += syms[i]
            num -= val[i]
        i += 1
    return res

n = int(input())
romans = input().split()

sum_total = sum(roman_to_int(r) for r in romans)

if sum_total > 3999:
    print("ERROR")
else:
    print(int_to_roman(sum_total))
0