목차

Secret Sharing

ps
링크acmicpc.net/…
출처BOJ
문제 번호2385
문제명Secret Sharing
레벨플래티넘 2
분류

그리디

시간복잡도O(mnlogn)
인풋사이즈n<=100, m<=5
사용한 언어Python
제출기록31908KB / 100ms
최고기록52ms
해결날짜2021/06/01

풀이

코드

"""Solution code for "BOJ 2385. Secret Sharing".

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

import functools


def main():
    N = int(input())  # pylint: disable=unused-variable
    shares = input().split()

    comp_key = functools.cmp_to_key(lambda x, y: -1 if (x + y) < (y + x) else 1)
    zero_shares = [x for x in shares if x[0] == '0']
    nonzero_shares = [x for x in shares if x[0] != '0']
    if not nonzero_shares:
        print('INVALID')
        return

    zero_part = ''.join(sorted(zero_shares, key=comp_key))
    z = zero_part[:5]
    first_part = min(
        nonzero_shares,
        key=functools.cmp_to_key(
            lambda x, y: -1 if (x + z + y) < (y + z + x) else 1))
    nonzero_shares.remove(first_part)
    last_part = ''.join(sorted(nonzero_shares, key=comp_key))

    print(first_part + zero_part + last_part)


if __name__ == '__main__':
    main()