Company: Visa_5_feb
Difficulty: medium
Market Discount Market Discount Description You are given a list of item prices at a market. The store has a special discount rule: The first item is purchased at full price. For every subsequent item, the discount applied is equal to the lowest price among all previously purchased items. The final price of an item cannot go below 0. Items must be purchased in the given order. Your task is to calculate the total cost of buying all items under this pricing rule. Examples Example 1: Input: prices = [2, 5, 1, 4] Output: 8 Explanation: First item costs 2 (no discount applies). Previously purchased prices: [2]. Second item costs 5 - 2 = 3 (minimum among previously purchased [2] is 2). Previously purchased prices: [2, 5]. Third item costs max(1 - 2, 0) = 0 (minimum among previously purchased [2, 5] is 2). Previously purchased prices: [2, 5, 1]. Fourth item costs 4 - 1 = 3 (minimum among previously purchased [2, 5, 1] is 1). Total cost: 2 + 3 + 0 + 3 = 8 Example 2: Input: prices = [4, 9, 2, 3