1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import sys dp = [[0] * 5001 for _ in range(5001)] n = int(sys.stdin.readline().rstrip()) arr = list(map(int, sys.stdin.readline().rstrip().split())) reverse = arr[::–1] MAX = –1 for i in range(0, n + 1): for j in range(0, n + 1): if i == 0 or j == 0: dp[i][j] = 0 elif arr[i – 1] == reverse[j – 1]: dp[i – 1][j – 1] = dp[i – 2][j – 2] + 1 MAX = max(dp[i – 1][j – 1], MAX) else: dp[i – 1][j – 1] = max(dp[i – 2][j – 1], dp[i – 1][j – 2]) print(n – MAX) | cs |