목차

수집합

ps
링크acmicpc.net/…
출처BOJ
문제 번호4373
문제명수집합
레벨골드 1
시간복잡도O(n^2)
인풋사이즈n<=1000
사용한 언어Python
제출기록153824KB / 1176ms
최고기록1176ms
해결날짜2022/05/13

풀이

코드

"""Solution code for "BOJ 4373. 수집합".

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

import collections
import itertools
import sys

INF = float('inf')


def main():
    while True:
        n = int(sys.stdin.readline())
        if n == 0:
            break
        S = [int(sys.stdin.readline()) for _ in range(n)]

        answer = -INF
        pairs_by_sum = collections.defaultdict(list)
        for a, b in itertools.combinations(S, 2):
            pairs_by_sum[a + b].append({a, b})
        for d in S:
            if d <= answer:
                continue
            for c in S:
                if c == d:
                    continue
                cd = {c, d}
                ab_list = pairs_by_sum[d - c]
                if any(not (ab & cd) for ab in ab_list):
                    answer = d
                    break
                
        print('no solution' if answer == -INF else answer)


if __name__ == '__main__':
    main()