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()