Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. from functools import lru_cache
  2.  
  3.  
  4. # lru_cache(None) mémoise la fonction
  5.  
  6. for _ in range(int(input())):
  7.     k, n = int(input()), int(input())
  8.     *a, = map(int, input().split())
  9.  
  10.     @lru_cache(None)
  11.     def ans(k, n):
  12.         """
  13.        profit maximum en utilisant au plus
  14.        k transations et avec la dernière
  15.        vente en n-1
  16.        """
  17.         if n < 2:
  18.             return 0
  19.         if k < 1:
  20.             return 0
  21.         r = ans(k - 1, n)
  22.         e = a[n - 1]
  23.         # ms: profit maximum jusqu'à l'indice
  24.         # s non inclus
  25.         ms = 0
  26.         for s in range(n - 1):
  27.             ms = max(ms, ans(k - 1, s))
  28.             r = max(ms + e - a[s], r)
  29.         return r
  30.     print(max(ans(k, i) for i in range(n+1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement