목차

Equator

ps
링크acmicpc.net/…
출처BOJ
문제 번호10274
문제명Equator
레벨골드 5
분류

dp

시간복잡도O(n)
인풋사이즈n<=1,000,000
사용한 언어Python 3.11
제출기록150212KB / 968ms
최고기록804ms
해결날짜2023/07/27

풀이

코드

"""Solution code for "BOJ 10274. Equator".

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

Tags: [dp]
"""

INF = float('inf')


def maximum_subarray(nums):
    max_sum, cur_max_sum = -INF, 0
    min_sum, cur_min_sum = INF, 0
    for num in nums:
        cur_max_sum = max(num, cur_max_sum + num)
        max_sum = max(max_sum, cur_max_sum)
        cur_min_sum = min(num, cur_min_sum + num)
        min_sum = min(min_sum, cur_min_sum)

    return max(0, max_sum), min(0, min_sum)


def main():
    T = int(input())
    for _ in range(T):
        _, *c = [int(x) for x in input().split()]
        max_sum, min_sum = maximum_subarray(c)
        print(max(max_sum, sum(c) - min_sum))


if __name__ == '__main__':
    main()