forked from tarun620/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumSumIncreasingSubsequence.py
59 lines (49 loc) · 1.43 KB
/
MaximumSumIncreasingSubsequence.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/python3
import sys
from copy import copy
fstd = "test2"
sys.stdin = open('%s.txt' % fstd, 'r')
input = sys.stdin.readline
# DP based Python
# implementation of Maximum Sum
# Increasing Subsequence (MSIS)
# problem
# constructMaxSumIS() returns the tupple:
# ( max sum of increasing subsequence, found subsequence )
def constructMaxSumIS(arr):
N = len(arr)
# L[i] stores the Maximum Sum Increasing Subsequence that ends with arr[i]
L = [[] for _ in range(N)]
# L[0] is equal to arr[0]
L[0].append(arr[0])
# msis[i] stores the Maximum Sum of Increasing Subsequence that
# ends with arr[i]
msis = [0] * N
# Initialize msis values
msis[0] = arr[0]
# Compute maximum sum
# values in bottom up manner
for i in range(1, N):
for j in range(i):
if (arr[i] > arr[j]) and (msis[i] < msis[j]):
msis[i] = msis[j]
L[i] = copy(L[j])
L[i].append(arr[i])
msis[i] += arr[i]
# Find max
maxIndex = max(range(N), key=lambda i: msis[i])
return (msis[maxIndex], L[maxIndex])
if __name__ == "__main__":
print("Input sequence:")
A = []
while (True):
line = input().strip()
try:
num = int(line)
except ValueError:
break
A.append(num)
print(A)
res = constructMaxSumIS(A)
print("Max sum is {}".format(res[0]))
print("Found sequence is: {} ".format(res[1]))