You are given predicted sale of N consecutive days of sweet shop and an amount K that is spend when shop is open for one day. Find total profit that can be earned when you are allowed to keep shop closed on any number of days (possibly zero).
You will earn profit on a day when sale is greater than the amount spend to keep the shop open. So find sum of all Ai - K such that Ai > K.
You are given predicted sale on N days and amount K is spend when you keep the shop open for 1 day. So profit on each day equals to Ai – K. So whenever the sale is less than K then it is not profitable to keep the shop open, hence you will keep the shop closed on those days. So the total profit will be sum of all Ai - K such that Ai > K.
Author’s solution can be found here.