Advertisement
Guest User

Fair Cut

a guest
Sep 24th, 2016
3,610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. n, k = map(int, input().split())
  2. if k > n // 2:
  3.     k = n - k
  4.  
  5. # sort the array, so that when processing ith number in a, we know a[i] is
  6. # larger than or equal to previously processed numbers, so calculating abs
  7. # is easier
  8. a = sorted(map(int, input().split()))
  9.  
  10. # dp[i][j] is the value when ith number in a has already processed, and j of
  11. # those numbers assigned to li, initialize all value to maximum number
  12. dp = [[float('inf') for i in range(n + 1)] for j in range(n + 1)]
  13.  
  14. # When i==0, j==0, no number assigned, the value is zero
  15. dp[0][0] = 0
  16.  
  17.  
  18. for i in range(0, n):  # iter through a
  19.     # iter through the possiblities of sizes in li when a[i] needs to be
  20.     # assigned
  21.     for j in range(0, i + 1):
  22.  
  23.         # We ignore the cases when the number of li or lu larger than required
  24.         if j > k or i - j > n - k:
  25.             continue
  26.  
  27.         # There are two possiblities: (1)assign a[i] to li (2) or to lu
  28.  
  29.         # Possiblity (1) assign to li:
  30.         # If a[i] would be assigned to li:
  31.         #       all the numbers in lu needs to be substracted from a[i],
  32.         #       so we add (i -j)* a[i] to d[i][j].
  33.         #       Note: substraction of all lu has been done in prevous steps
  34.         #       (see below, 3 lines later)
  35.         #       (i-j) is the current number in lu.
  36.         #
  37.         #       a[i] WILL be substracted from all future a[x] assigned to lu,
  38.         #       so we substract (n - k - (i - j))*a[i] from d[i][j]
  39.         #       (n-k): size of lu in the final step,
  40.         #       (i-j): current number in lu
  41.         #       (n-k -(i-j)): size of remaining numbers to be assigned to lu
  42.  
  43.         temp_li = dp[i][j] + a[i] * (i - j - (n - k - (i - j)))
  44.  
  45.         # Possiblity (2) assign to lu:
  46.         # If a[i] would be assigned to lu:
  47.         #       all the numbers in li needs to be substracted from a[i],
  48.         #       so we add j* a[i] to d[i][j]
  49.         #       Note: substraction of all lu has been done in prevous steps
  50.         #       (see below, 2 lines later)
  51.         #
  52.         #       a[i] WILL be substracted from all future a[x] assigned to li,
  53.         #       so we substract (k-j)*a[i] from d[i][j]
  54.         #       (k-j) is the size of remaining numbers to be assigned to li
  55.         temp_lu = dp[i][j] + a[i] * (j - (k - j))
  56.  
  57.         # Possiblity (1), both sizes of assigned numbers and li increment by 1
  58.         if dp[i + 1][j + 1] > temp_li:
  59.             dp[i + 1][j + 1] = temp_li
  60.  
  61.         # Possiblity (2), size of assigned numbers increment by 1, size of li
  62.         # remains the same
  63.         if dp[i + 1][j] > temp_lu:
  64.             dp[i + 1][j] = temp_lu
  65.  
  66. print(dp[n][k])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement