fork download
  1. Solution:
  2.  
  3. The first two observations can be understood with this pseudocode:
  4. -------------------------------BRUTE FORCE--------------------------------------------------------
  5. if m > n print -1
  6. else
  7. for each shop
  8. if the current shop has an item, purchase it
  9. if m is equal to the number of items purchased, break out of the loop
  10. if no item has been purchased, increase the time by one second.
  11. else, if items have been purchased,
  12. increase the time by the number of items purchased times k
  13. end for each
  14. print total time
  15.  
  16. -------------------------------------------------------------------------------------------------
  17. The sliding window solution(`v` contain the indices of only the shops that sell) ->
  18.  
  19. for each i from m to r:
  20. diff = v[i-m+1]-v[i-m] - (v[i-1]-v[i-m]) * k + (v[i]-v[i-1]) * (m-1) * k;
  21. ans = ans + diff;
  22. final_ans = min(final_ans, ans)
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty