-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax Array Sum.py
More file actions
37 lines (27 loc) · 758 Bytes
/
Copy pathMax Array Sum.py
File metadata and controls
37 lines (27 loc) · 758 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the maxSubsetSum function below.
def maxSubsetSum(arr):
if len(arr) == 0:
return 0
elif len(arr) == 1:
return arr[0]
elif len(arr) == 2:
return max(arr[0], arr[1])
dp = [0] * len(arr)
dp[0] = arr[0]
dp[1] = max(arr[0], arr[1])
for i in range(2, len(arr)):
dp[i] = max(arr[i], dp[i-1], arr[i] + dp[i-2])
return dp[-1]
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()