def solve_lis(arr):
n = len(arr)
best = [0] * (n+1)
best[n-1] = 1
# step 1 we'll compute the best array using Dynamic Programming
for i in range(n-2, -1, -1):
max = 0
temp = arr[i]
for j in range(i+1, n):
if arr[j] > arr[i] and best[j] > max:
max = best[j]
best[i] = 1 + max
maxEl = best[0]
maxPos = 0
for i in range(1, n):
if best[i] > maxEl:
maxEl = best[i]
maxPos = i
print(arr[maxPos], end = " ")
maxEl -= 1
for k in range(maxPos+1, n):
if best[k] == maxEl and arr[maxPos] < arr[k]:
print("%d "%arr[k], end =" ")
maxEl-=1
def func():
# let we have the following array
arr = [10, 22, 9, 33, 21, 50, 41, 60, 80]
solve_lis(arr)
func()
ZGVmIHNvbHZlX2xpcyhhcnIpOgogICAgbiA9IGxlbihhcnIpCiAgICBiZXN0ID0gWzBdICogKG4rMSkKICAgIGJlc3Rbbi0xXSA9IDEKCiAgICAjIHN0ZXAgMSB3ZSdsbCBjb21wdXRlIHRoZSBiZXN0IGFycmF5IHVzaW5nIER5bmFtaWMgUHJvZ3JhbW1pbmcKICAgIGZvciBpIGluIHJhbmdlKG4tMiwgLTEsIC0xKToKICAgICAgICBtYXggPSAwCiAgICAgICAgdGVtcCA9IGFycltpXQogICAgICAgIGZvciBqIGluIHJhbmdlKGkrMSwgbik6CiAgICAgICAgICAgIGlmIGFycltqXSA+IGFycltpXSBhbmQgYmVzdFtqXSA+IG1heDoKICAgICAgICAgICAgICAgIG1heCA9IGJlc3Rbal0KICAgICAgICBiZXN0W2ldID0gMSArIG1heAoKICAgIG1heEVsID0gYmVzdFswXQogICAgbWF4UG9zID0gMAogICAgZm9yIGkgaW4gcmFuZ2UoMSwgbik6CiAgICAgICAgaWYgYmVzdFtpXSA+IG1heEVsOgogICAgICAgICAgICBtYXhFbCA9IGJlc3RbaV0KICAgICAgICAgICAgbWF4UG9zID0gaQogICAgcHJpbnQoYXJyW21heFBvc10sIGVuZCA9ICIgIikKICAgIG1heEVsIC09IDEKICAgIGZvciBrIGluIHJhbmdlKG1heFBvcysxLCBuKToKICAgICAgICBpZiBiZXN0W2tdID09IG1heEVsIGFuZCBhcnJbbWF4UG9zXSA8IGFycltrXToKICAgICAgICAgICAgcHJpbnQoIiVkICIlYXJyW2tdLCBlbmQgPSIgIikKICAgICAgICAgICAgbWF4RWwtPTEKZGVmIGZ1bmMoKToKICAgICMgbGV0IHdlIGhhdmUgdGhlIGZvbGxvd2luZyBhcnJheQogICAgYXJyID0gWzEwLCAyMiwgOSwgMzMsIDIxLCA1MCwgNDEsIDYwLCA4MF0KCiAgICBzb2x2ZV9saXMoYXJyKQoKZnVuYygpCg==