Solution:
The first two observations can be understood with this pseudocode:
-------------------------------BRUTE FORCE--------------------------------------------------------
if m > n print -1
else
for each shop
if the current shop has an item, purchase it
if m is equal to the number of items purchased, break out of the loop
if no item has been purchased, increase the time by one second.
else, if items have been purchased,
increase the time by the number of items purchased times k
end for each
print total time
-------------------------------------------------------------------------------------------------
The sliding window solution(`v` contain the indices of only the shops that sell) ->
for each i from m to r:
diff = v[i-m+1]-v[i-m] - (v[i-1]-v[i-m]) * k + (v[i]-v[i-1]) * (m-1) * k;
ans = ans + diff;
final_ans = min(final_ans, ans)
U29sdXRpb246CgpUaGUgZmlyc3QgdHdvIG9ic2VydmF0aW9ucyBjYW4gYmUgdW5kZXJzdG9vZCB3aXRoIHRoaXMgcHNldWRvY29kZToKLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLUJSVVRFIEZPUkNFLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KaWYgbSA+IG4gcHJpbnQgLTEKZWxzZQogICAgZm9yIGVhY2ggc2hvcAogICAgICAgIGlmIHRoZSBjdXJyZW50IHNob3AgaGFzIGFuIGl0ZW0sIHB1cmNoYXNlIGl0CiAgICAgICAgaWYgbSBpcyBlcXVhbCB0byB0aGUgbnVtYmVyIG9mIGl0ZW1zIHB1cmNoYXNlZCwgYnJlYWsgb3V0IG9mIHRoZSBsb29wCiAgICAgICAgaWYgbm8gaXRlbSBoYXMgYmVlbiBwdXJjaGFzZWQsIGluY3JlYXNlIHRoZSB0aW1lIGJ5IG9uZSBzZWNvbmQuCiAgICAgICAgZWxzZSwgaWYgaXRlbXMgaGF2ZSBiZWVuIHB1cmNoYXNlZCwKICAgICAgICAgICAgICBpbmNyZWFzZSB0aGUgdGltZSBieSB0aGUgbnVtYmVyIG9mIGl0ZW1zIHB1cmNoYXNlZCB0aW1lcyBrCiAgICBlbmQgZm9yIGVhY2gKICAgIHByaW50IHRvdGFsI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