#
# Dynamic Programming
#
def find_lis(arr):
n = len( arr )
best = [0] * (n)
best[n-1] = 1
# we will create the best array
for i in range(n-2, -1, -1):
aux = arr[i]
max = 0
for j in range(i+1, n):
if arr[j] > aux and best[j] > max:
max = best[j]
best[i] = 1 + max
maxBest = best[0]
posMax = 0
for i in range(1, n):
if best[i]>maxBest:
maxBest = best[i]
posMax = i
print(best)
print(maxBest, posMax)
print(arr[posMax], end = " ")
pos = maxBest
pos-=1
for i in range(posMax+1, n):
if best[i] == pos and arr[i] > arr[posMax]:
print(arr[i], end = " ")
pos-=1
def gokyo():
arr = [24,12,15,15,19]
print(arr)
find_lis(arr)
gokyo()
IwojIER5bmFtaWMgUHJvZ3JhbW1pbmcKIwpkZWYgZmluZF9saXMoYXJyKToKCiAgICBuID0gbGVuKCBhcnIgKQoKICAgIGJlc3QgPSBbMF0gKiAobikKICAgIGJlc3Rbbi0xXSA9IDEKCiAgICAjIHdlIHdpbGwgY3JlYXRlIHRoZSBiZXN0IGFycmF5CiAgICBmb3IgaSBpbiByYW5nZShuLTIsIC0xLCAtMSk6CiAgICAgICAgYXV4ID0gYXJyW2ldCiAgICAgICAgbWF4ID0gMAogICAgICAgIGZvciBqIGluIHJhbmdlKGkrMSwgbik6CiAgICAgICAgICAgIGlmIGFycltqXSA+IGF1eCBhbmQgYmVzdFtqXSA+IG1heDoKICAgICAgICAgICAgICAgIG1heCA9IGJlc3Rbal0KICAgICAgICBiZXN0W2ldID0gMSArIG1heAogICAgbWF4QmVzdCA9IGJlc3RbMF0KICAgIHBvc01heCA9IDAKICAgIGZvciBpIGluIHJhbmdlKDEsIG4pOgogICAgICAgIGlmIGJlc3RbaV0+bWF4QmVzdDoKICAgICAgICAgICAgbWF4QmVzdCA9IGJlc3RbaV0KICAgICAgICAgICAgcG9zTWF4ID0gaQogICAgcHJpbnQoYmVzdCkKICAgIHByaW50KG1heEJlc3QsIHBvc01heCkKICAgIHByaW50KGFycltwb3NNYXhdLCBlbmQgPSAiICIpCiAgICBwb3MgPSBtYXhCZXN0CiAgICBwb3MtPTEKICAgIGZvciBpIGluIHJhbmdlKHBvc01heCsxLCBuKToKICAgICAgICBpZiBiZXN0W2ldID09IHBvcyBhbmQgYXJyW2ldID4gYXJyW3Bvc01heF06CiAgICAgICAgICAgIHByaW50KGFycltpXSwgZW5kID0gIiAiKQogICAgICAgICAgICBwb3MtPTEKCmRlZiBnb2t5bygpOgoKICAgIGFyciA9IFsyNCwxMiwxNSwxNSwxOV0KICAgIHByaW50KGFycikKICAgIGZpbmRfbGlzKGFycikKCmdva3lvKCkK