Dynamic Programming > Max Array Sum

이희진·2022년 12월 2일
0
#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the maxSubsetSum function below.
def maxSubsetSum(arr):
    if len(arr) == 1:
        return 0
    n1, n2 = arr[0], max(arr[:2])
    for num in arr[2:]:
        n3 = max(n1+num, n2, num)
        n1, n2 = n2, n3
    return n3

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    arr = list(map(int, input().rstrip().split()))

    res = maxSubsetSum(arr)

    fptr.write(str(res) + '\n')

    fptr.close()

0개의 댓글