목차

용액 합성하기

ps
링크acmicpc.net/…
출처BOJ
문제 번호14921
문제명용액 합성하기
레벨골드 5
분류

투 포인터

시간복잡도O(n)
인풋사이즈n<=100,000
사용한 언어Python
제출기록40768KB / 120ms
최고기록88ms
해결날짜2021/09/17

풀이

코드

"""Solution code for "BOJ 14921. 용액 합성하기".

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

Tags: [TwoPointer]
"""

INF = float('inf')


def main():
    N = int(input())  # pylint: disable=unused-variable
    nums = [int(x) for x in input().split()]

    from_left, from_right = iter(nums), reversed(nums)
    left, right = next(from_left), next(from_right)
    answer = INF
    for _ in range(N - 1):
        sum_val = left + right
        if abs(sum_val) < abs(answer):
            answer = sum_val
        if sum_val > 0:
            right = next(from_right)
        else:
            left = next(from_left)

    print(answer)


if __name__ == '__main__':
    main()