what is the wrong with my Dividing Sequences code
<p>I am trying to code this problem: </p>

<blockquote>
  <p>This problem is about sequences of positive integers a1,a2,...,aN. A
  subsequence of a sequence is anything obtained by dropping some of the
  elements. For example, 3,7,11,3 is a subsequence of
  6,3,11,5,7,4,3,11,5,3 , but 3,3,7 is not a subsequence of
  6,3,11,5,7,4,3,11,5,3 .</p>
  
  <p>Given a sequence of integers your aim is to find the length of the longest fully dividing subsequence of this sequence.</p>
  
  <p>A fully dividing sequence is a sequence a1,a2,...,aN where ai divides
  aj whenever i &lt; j. For example, 3, 15, 60, 720 is a fully dividing
  sequence.</p>
</blockquote>

<p>My code is:</p>

<pre><code>n=input()
ar=[]

temp=0
for i in range (0,n):
    temp=input()
    ar.append(temp)

def best(i):
    if i==0:
        return (1)
    else:
        ans =1
        for j in range (0,i):
            if (ar[j]%ar[i]==0):

                ans=max(ans,(best(j)+1))
         return (ans)
an=[]
for i in range (0,n):
   temp=best(i)
   an.append(temp)

print max(an)
</code></pre>

<p>the input was </p>

<pre><code>9
2 
3 
7 
8 
14 
39 
145 
76 
320
</code></pre>

<p>and I should get 3 (because of 2, 8, 320) as output but I am getting 1</p>

<p>This solves your problem without using any recursion :) </p>

<pre><code>n = int(input())
ar = []
bestvals = []
best_stored = []
for x in range(n):
  ar.append(int(input()))
  best_stored.append(0)

best_stored[0] = 1

for i in range(n):
  maxval = 1
  for j in range(i):
    if ar[i] % ar[j] == 0:
      maxval = max(maxval,(best_stored[j])+1)
  best_stored[i] = maxval

print(max(best_stored))
</code></pre>

python algorithm