a shop sell candy discount for candy sell shop give candy freeThe customer choose candy away free long cost choose candy equal minimum cost candy boughtfor example 4 candy cost 1 2 3 4 customer buy candy cost 2 3 candy cost 1 free candy cost 4given 0indexed integer array cost costi denote cost ith candy return minimum cost buying candy example 1input cost 123output 5explanation we buy candy cost 2 3 candy cost 1 freethe total cost buying candy 2 3 5 this way buy candiesnote buy candy cost 1 3 candy cost 2 freethe cost free candy equal minimum cost purchase candiesexample 2input cost 657922output 23explanation the way minimum cost describe Buy candy cost 9 7 take candy cost 6 free we buy candy cost 5 2 take remain candy cost 2 freehence minimum cost buy candy 9 7 5 2 23example 3input cost 55output 10explanation since 2 candy buy there candy freehence minimum cost buy candy 5 5 10 constraints1 costlength 1001 costi 100