Welcome to the Magical Fruit Market! ๐๐๐
You're at a special fruit market where each fruit has a unique pricing system. You are given a 0-indexed integer array prices where prices[i] represents the number of coins needed to purchase the (i + 1)th fruit.
Here's the magical twist: When you purchase the (i + 1)th fruit at prices[i] coins, you receive a special reward - you can get any number of the next i fruits for free!
Important notes:
- Even if you can take a fruit for free, you can still choose to purchase it to receive its reward
- The reward for fruit
i+1gives you free access to the nextifruits (noti+1fruits) - You need to acquire all fruits in the market
Goal: Return the minimum number of coins needed to acquire all the fruits.
Example: If prices = [3,1,2], buying fruit 1 (costs 3 coins) gives you 0 free fruits, buying fruit 2 (costs 1 coin) gives you 1 free fruit, and buying fruit 3 (costs 2 coins) gives you 2 free fruits.
Input & Output
Constraints
- 1 โค prices.length โค 1000
- 1 โค prices[i] โค 1000
- Each fruit must be acquired exactly once
- Free fruits from purchase i can be used for fruits (i+1) to (i+1)+i