Maximum Number of Eaten Apples
There is a special kind of apple tree that grows apples every day for n days. On the i^th day, the tree grows apples[i] apples that will rot after days[i] days; that is, on day i + days[i] the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, denoted by apples[i] == 0 and days[i] == 0.
You decided to eat at most one apple a day. Note that you can keep eating after the first n days.
Given two integer arrays days and apples of length n, return the maximum number of apples you can eat.
Example 1
Input
apples = [1,2,3,5,2], days = [3,2,1,4,2]Output
7By choosing apples before they rot, you can eat 1 apple from day 1, 2 apples from day 2, and 4 apples from day 4, for a total of 7.
Example 2
Input
apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]Output
5You can eat the 3 apples grown on the first day, do nothing on the fourth and fifth days, then eat the 2 apples grown on the sixth day.
Constraints
- n == apples.length == days.length
- 1 <= n <= 2 * 10^4
- 0 <= apples[i], days[i] <= 2 * 10^4
- days[i] = 0 if and only if apples[i] = 0.