title: Maximum sum of subsequence of length L with a restriction
body: <p>Given an array of positive integers. How to find a subsequence of length <code>L</code> with <code>max</code> sum which has the distance between any two of its neighboring elements that do not exceed <code>K</code></p>
  
  <p>I have the following solution but don't know how to take into account length L.  </p>
  
  <p>1 &lt;= N &lt;= 100000, 1 &lt;= L &lt;= 200, 1 &lt;= K &lt;= N</p>
  
  <p>f[i] contains max sum of the subsequence that ends in i.</p>
  
  <pre><code>for i in range(K, N)
      f[i] = INT_MIN
      for j in range(1, K+1)
          f[i] = max(f[i], f[i-j] + a[i])
  return max(f)
  </code></pre>