Guest User

Ball Elimination

a guest
Mar 12th, 2017
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. INF = (1<<30)
  2.  
  3. def f(i, j):
  4.     if dp[i][j] is not None:
  5.         return dp[i][j]
  6.    
  7.     if i==j+1:
  8.         res = 0
  9.     elif i==j:
  10.         res = 1
  11.     else:
  12.         res = INF
  13.         if a[i]==a[j]:
  14.             res = f(i+1, j-1)
  15.         else:
  16.             for k in range(i, j):
  17.                 res = min(res, f(i, k)+f(k+1, j))
  18.     dp[i][j] = res
  19.     return dp[i][j]
  20.  
  21. n = int(input())
  22. a = [int(w) for w in input().split()]
  23.  
  24. dp = [[None]*n for i in range(n)]
  25.  
  26. ans = f(0, n-1)
  27. print(ans)
Add Comment
Please, Sign In to add comment