백준 1431번: 시리얼 번호 #Python

ColorlessDia·2025년 4월 19일

algorithm/baekjoon

목록 보기
517/836
import sys

input = sys.stdin.readline

N = int(input())

serial_dict = dict()

for _ in range(N):
    serial = input().rstrip()

    length = len(serial)
    sum_number = sum([int(char) for char in serial if char.isnumeric()])

    if length not in serial_dict:
        serial_dict[length] = []
    
    serial_dict[length] += [(sum_number, serial)]

for _, v in sorted(serial_dict.items(), key=lambda x: x[0]):
    sorted_v = sorted(v, key=lambda x: (x[0], x[1]))
    
    for _, sorted_serial in sorted_v:
        print(sorted_serial)

0개의 댓글