목차

36진법

ps
링크acmicpc.net/…
출처BOJ
문제 번호1036
문제명36진수
레벨골드 1
분류

그리디

시간복잡도O(nm)
인풋사이즈n<=50, m<=50
사용한 언어Python
제출기록29200KB / 80ms
최고기록56ms
해결날짜2021/06/03

풀이

코드

"""Solution code for "BOJ 1036. 36진법".

- Problem link: https://www.acmicpc.net/problem/1036
- Solution link: http://www.teferi.net/ps/problems/boj/1036
"""

DIGITS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'


def to_base36(num):
    if num == 0:
        return '0'
    ret = []
    while num:
        num, r = divmod(num, 36)
        ret.append(DIGITS[r])
    return ''.join(reversed(ret))


def main():
    N = int(input())
    nums = [input() for x in range(N)]
    K = int(input())

    dec = {x: i for i, x in enumerate(DIGITS)}    
    increase = [0] * 36
    dec_sum = 0
    for num in nums:
        r = 1
        num_in_dec = 0
        for ch in reversed(num):
            ch_in_dec = dec[ch]
            num_in_dec += ch_in_dec * r
            increase[ch_in_dec] += (35 - ch_in_dec) * r
            r *= 36
        dec_sum += num_in_dec
    total_increase = sum(sorted(increase, reverse=True)[:K])
    answer = to_base36(dec_sum + total_increase)
    
    print(answer)
    

if __name__ == '__main__':
    main()