백준 14002 - 가장 긴 증가하는 부분 수열 4 (파이썬)

Updated:

Answer

from sys import stdin

input = stdin.readline

N = int(input())
a = list(map(int, input().split()))
dp = [1] * N

for i in range(1, N):
    for j in range(i):
        if a[i] > a[j]:
            dp[i] = max(dp[i], dp[j] + 1)

tmp = max(dp)
print(tmp)
b = []

for i in range(N - 1, -1, -1):
    if dp[i] == tmp:
        b.append(a[i])
        tmp -= 1

for i in b[::-1]:
    print(i, end = ' ')

Categories:

Updated: