We need to solve: given n trees, each tree i has ripen day a_i and number of fruits b_i. Fruits can be collected on day a_i or a_i+1 only, each day Valera can collect at most v fruits total. Maximize total collected.
We can think of days up to max day+1 (since a_i+1). Let max_day = max(a_i) + 1. We have constraints per day: collect at most v. Also each tree's fruits are available on two specific days. This is like flow: each tree's fruits supply b_i, but can be allocated to two days: day a_i and a_i+1. Each day has capacity v. Want maximize total allocated.
Since n, v, a_i, b_i up to 3000, we can simulate greedily. Classic problem: "Harvest of Fruits" or "Ladders". Approach: sort trees by day? Or process days sequentially, for each day, collect as much as possible from fruits that are available that day (prefer those that are about to expire? Actually fruits that are available on that day: those that ripen on that day (first day) and those that ripen on previous day (second day). Since fruits that ripen on day d are available on d and d+1. For day d, we have two sources: trees with a_i = d (first day), and trees with a_i = d-1 (second day) that still have leftover fruits not collected on day d-1.
We need to decide how many to collect from each. A standard greedy: iterate days from 1 to max_day (or maybe from 1 to something). At each day d, we have capacity v. First, we should collect from fruits that are about to expire: i.e., those from day d-1 that are still left (since they will be gone after today). Then if capacity remains, collect from new fruits of day d.
Because if we postpone collecting from day d's fruits to day d+1, it's still possible; but day d-1 fruits will be wasted if not collected today. So it's optimal to always prioritize the "yesterday" trees over today's.
Implementation: We can keep an array fruits_at_day for first day? Actually we can store for each day, the total fruits that become ripe on that day, call ripe[d] = sum of b_i for trees with a_i = d. Then also maintain leftover from previous day? But careful: each tree's fruits are distinct; but we can treat them aggregated per day if we consider that after we collect some on the first day, the remainder becomes available on next day. So we can keep an array carryover that holds the amount of fruits that are from previous day and not collected yet.
Standard solution: Initialize ans = 0. For each day d from 1 to max_day (maybe up to 3000+1), we have capacity = v. Let from_yesterday = carryover (the leftover from day d-1 after day d-1 collection). But actually, after day d-1, we might have some fruits that were not collected from those that ripened on day d-1? And those will be available on day d (d = (d-1)+1). So we can define variable left_from_previous = amount of fruits that are still available from day d-1 (i.e., from trees with a_i = d-1, after collecting on day d-1). That is exactly what we need to know.
We can maintain an array leftover[day]? Or just a variable because we process sequentially.
Approach: Let an array ripe[1..max_day+1] initialized to 0. For each tree, add b_i to ripe[a_i]. Then for day d from 1 to max_day (where max_day = max(a_i)+1), we have capacity v. First, take as many as possible from the fruits that were left from previous day (call carry). Let take1 = min(carry, v). ans += take1; capacity left = v - take1; carry -= take1 (but we should set carry to 0 because we consumed). Actually after collecting take1 from carry, we set carry = 0? But careful: The carry is from previous day's ripe that remained after previous day's collection. But we might also have leftover from earlier days? Actually fruits are only available on two consecutive days, so any fruit from day d-2 is no longer available. So we only need to keep the carry from immediate previous day. So we can store it as a variable prev_left (fruits from day d-1 that were not collected on day d-1). At start of day d, we have prev_left (>=0). We take from it.
After that, we have new fruits ripe[d] (fruits that ripen on day d) that can be collected today or tomorrow. Let take2 = min(ripe[d], v - take1). ans += take2; now after day d, any fruits from ripe[d] that were not collected today will be available tomorrow as leftovers. So we need to set the next day's prev_left = ripe[d] - take2. But careful: The leftover from today's ripe will be available tomorrow. However, we might also have prev_left? But we already consumed all of prev_left (we took min(prev_left, v) and if prev_left > v then we could only take v, leaving some prev_left? But we only had capacity v, we consumed take1 from prev_left. If prev_left > v, then we cannot take all prev_left because we have capacity v, but we would take v all from prev_left, leaving prev_left - v >0. However, that leftover from prev_left cannot be taken tomorrow? But it would be from day d-1, which after day d (i.e., day d is the second day for those fruits) they expire. So they cannot be collected tomorrow. So we must ensure that we take as much as possible from prev_left, but if prev_left > v, we can only take v, leaving the rest wasted. But wait: Is it possible that prev_left > v? Because we had day d-1 collection: we had capacity v, we would have collected as much as possible from day d-1's prev_left (from d-2) and then from ripe[d-1]. The leftover from day d-1's ripe[d-1] (i.e., that we didn't collect on day d-1) becomes prev_left for day d. So prev_left = leftover from ripe[d-1] after day d-1's collection. The maximum that leftover could be? At day d-1, we had capacity v, and we may have taken from prev_left_at_d-1 (i.e., from ripe[d-2]) first, then from ripe[d-1]. The leftover from ripe[d-1] is max(0, ripe[d-1] - (v - take_from_prev)). That leftover could be more than v? Yes if ripe[d-1] > v. For example, v=3, ripe[d-1]=10, and we also had prev_left=2, then day d-1: take from prev: min(2,3)=2, remaining capacity 1, take from ripe[d-1] 1, leftover ripe[d-1] = 9. So prev_left for day d = 9. That's > v. So on day d, we have capacity v=3 and prev_left=9. We take min(prev_left, v)=3, but we cannot take all 9, so 6 will be wasted because after day d, they expire. So indeed we might have leftover from prev_left that is not collected.
Thus algorithm: For day d from 1 to max_day+1 perhaps, we maintain variable carry (fruits that ripened on day d-1 and are still available). At start of day, we have carry (maybe from previous iteration). Then we have capacity v. We take from carry: take1 = min(carry, v). ans += take1; capacity_rem = v - take1; then we have fresh = ripe[d] (if d <= max_day, else 0). take2 = min(fresh, capacity_rem). ans += take2; then after this day, the leftovers that will be available tomorrow are from fresh: leftover = fresh - take2. But what about carry? The part of carry that we didn't take is wasted, because it's last day. So we set carry = leftover for next day. However, note: There might also be fruits that ripened on day d-1 that we didn't take and are still in carry? But we already accounted: we started with carry = leftover from fresh of previous day, i.e., from ripe[d-1] after day d-1. We then took some. The part of carry that we didn't take is lost. So we discard it. So for next day, carry = leftover from today's fresh only. That's correct.
But we need to also consider the last day: after max_day, there might be fruits that ripened on max_day (which is max(a_i)) and have one more day (max_day+1) to be collected. So we need to process days up to max_day+1 inclusive. Because fruits from day max_day are available on max_day and max_day+1. So we need to have one extra day to collect them. So we set maxDay = max(a_i). Then we process day d from 1 to maxDay+1. For days beyond maxDay, ripe[d] = 0 (since no trees ripe on those days). But we might have carry from day maxDay, which we process on day maxDay+1.
Edge: There might be days with no ripe but we still need to process if carry >0? But since we process up to maxDay+1, that should cover all possible carry. After day maxDay+1, any leftover carry (from day maxDay's ripe) would be wasted because day maxDay+1 is the last day they can be collected. So we stop.
Thus algorithm:
- Find maxA = max(a_i).
- Create array ripe[1..maxA+2] initialize 0 (we need up to maxA+1 maybe, but we can just make size large, like 3005).
- For each tree: ripe[a_i] += b_i.
- ans = 0, carry = 0.
- For day = 1 to maxA+1:
capacity = v.
collect from carry (yesterday's leftovers)
take1 = min(carry, capacity)
ans += take1
capacity -= take1
collect from today's fresh
take2 = min(ripe[day], capacity)
ans += take2
leftover from today becomes carry for next day
carry = ripe[day] - take2
Note: we discard any leftover from carry (uncollected) as it expires.
Return ans.
Check with examples:
Example1:
n=2,v=3.
Trees: (1,5), (2,3)
maxA=2, process days 1..3.
ripe[1]=5, ripe[2]=3.
Initialize carry=0, ans=0.
Day1:
capacity=3
take1=min(0,3)=0
capacity remains 3
take2=min(5,3)=3, ans=3
carry=5-3=2
Day2:
capacity=3
take1=min(2,3)=2, ans=5, capacity=1
take2=min(3,1)=1, ans=6, capacity=0
carry=3-1=2
Day3:
capacity=3
take1=min(2,3)=2, ans=8, capacity=1
take2=min(ripe[3]=0,1)=0
carry=0.
Total=8. matches.
Example2:
n=5,v=10, all trees: (3,20),(2,20),(1,20),(4,20),(5,20)
maxA=5, process days 1..6.
ripe[1]=20, ripe[2]=20, ripe[3]=20, ripe[4]=20, ripe[5]=20.
carry=0.
Day1:
cap=10, take1=0, take2=min(20,10)=10, ans=10, carry=10.
Day2:
cap=10, take1=min(10,10)=10, ans=20, cap=0, take2=0, carry=20 (since ripe[2]=20, take2=0 => carry=20)
But careful: On day2, we took from carry (10 from day1's leftover) all capacity, so we couldn't collect any fresh from day2. So day2's ripe becomes carry for day3. So carry after day2 = 20.
Day3:
cap=10, take1=min(20,10)=10, ans=30, cap=0, take2=0, carry = ripe[3]=20 (since take2=0) -> Actually ripe[3]=20, but we didn't take any, so carry = 20.
Day4:
cap=10, take1=min(20,10)=10, ans=40, cap=0, take2=0, carry = ripe[4]=20.
Day5:
cap=10, take1=min(20,10)=10, ans=50, cap=0, take2=0, carry = ripe[5]=20.
Day6:
cap=10, take1=min(20,10)=10, ans=60, cap=0, take2=0, carry = 0 (since ripe[6]=0).
Total=60. matches.
Thus algorithm works.
But we need to be careful about day indices: a_i can be from 1 to 3000, but we might have days beyond 3000? Since a_i up to 3000, maxA+1 <= 3001. So we can allocate array of size 3002 or 3005.
Implementation details: We'll read n and v. Then we'll find maxA and fill ripe array. Since n up to 3000, we can do O(maxA) loop.
But is greedy always optimal? This is a standard flow problem with capacities per day and fruits per tree with two possible days. It can be solved by max flow but greedy works because it's like a bipartite matching with special structure. The priority rule is correct.
We should also consider if there could be trees with same a_i, but we aggregated them. That's fine because the fruits are indistinguishable for collection (only total matters). However, caution: The fruits from different trees with same a_i are independent; but we are aggregating, it's fine because they have same availability days. The greedy of taking from yesterday's leftovers first is optimal: If on day d we choose to take some from today's fruits while leaving yesterday's leftovers that could have been taken, then those leftovers will be wasted (since they expire after today) while today's fruits could be taken tomorrow. So swapping one unit from today to yesterday yields same or better outcome: use the capacity on day d to take leftover, and delay taking today's fruit to tomorrow (if capacity permits). However, if tomorrow's capacity is limited, it might be that delaying today's fruit may cause some of them to be wasted if tomorrow's capacity is already used by other leftovers or tomorrow's fruit. But note: The leftovers from today become available tomorrow. By taking from yesterday's leftovers today, we free capacity tomorrow for today's leftovers? Actually, we need to compare two strategies: On day d, we have capacity v. There are two groups: L = leftovers from day d-1 (must be taken today or lost), T = fresh from day d (can be taken today or tomorrow). Suppose we decide to take x from L and (v-x) from T. Then leftovers from T become (T - (v-x)). Those will be available tomorrow. Tomorrow, we have capacity v and also new fresh (tomorrow's ripe) and maybe other leftovers? Actually, tomorrow's leftovers will be from today's T leftovers, plus maybe leftovers from tomorrow's fresh if not taken? But we have to consider tomorrow's decision. The question is whether it's ever optimal to leave some L untaken (i.e., x < min(v, L)) to save capacity for T today? If we leave some L, they are lost, and we have taken more T today, which means less T leftovers to tomorrow. That might allow tomorrow to take more of tomorrow's fresh? But we can formalize: Suppose we reduce x by 1 (take one less from L today) and instead take one more from T today. Then we lose 1 fruit from L (since it expires), and we reduce T leftovers by 1 (since we took one extra). So net change: we gained 1 fruit (from T) but lost 1 from L, net 0? Actually careful: originally we would have taken that L fruit, now we don't take it; it's wasted. So we lose 1 fruit from L. And we take an extra T fruit, gaining 1 fruit. So net immediate change is 0. But the difference is that the amount of T leftovers for tomorrow decreases by 1 (since we took one more today). That means tomorrow there will be 1 less fruit from T available. However, we also have the same capacity tomorrow. The question is whether that extra T leftover would have been collected tomorrow or not. If tomorrow we have enough capacity to collect it, then reducing it might cause us to lose that fruit tomorrow, net effect -1. If tomorrow we would have not been able to collect it due to capacity constraints, then reducing it doesn't lose anything, net effect 0. So it's never beneficial to reduce x (take fewer from L) because that could only cause potential loss tomorrow, never gain. Because we replace taking L (which must be taken today) with taking T (which could be taken tomorrow). If tomorrow has enough capacity to take that T fruit, then we effectively shifted it from today to tomorrow, but lost the L fruit. So net we lost L fruit that could have been taken today, and T fruit is taken tomorrow instead of maybe something else. But if tomorrow's capacity is not fully used, then we could take that T fruit tomorrow anyway, so by taking it today we are just using capacity that could have been used for L today? Actually, consider scenario: v=1. Day d-1: left L=1 (from ripe[d-1] leftover). Day d: fresh T=1. Day d+1: fresh R=0. If we take L on day d, we get 1, then T leftover becomes 1 (since we didn't take T). On day d+1, we can take that T leftover (since no other fruit). So total = 2 (L+T). If we take T on day d instead of L, we get 1 from T, L wasted, and T leftover=0. Then total=1. So it's worse. So prioritizing L is better. In general, always take as much L as possible is optimal, because any unit not taken from L is definitely lost, while any unit of T can possibly be taken later. So greedy works.
Thus solution is correct.
Now constraints: a_i up to 3000, but what if a_i=3000, then we need day 3001. So we can allocate array size maybe 3002. Or we can just use dictionary but easier to preallocate 3002.
Edge Cases:
- Some trees may have a_i = 0? Problem says a_i >= 1.
- v could be larger than sum of all b_i; then ans = sum(b_i). Greedy will sum all as capacity is enough each day.
- Days may have no ripe, but if carry >0, we still collect.
- Maximum day: maybe we have a_i = 1 and no other days, then we need to process day 1 and day 2 (since fruits can be collected on day 2 as well). So we need to loop to maxA+1.
Thus we can implement.
Complexities: O(n + maxA). n, maxA <= 3000, trivial.
Now we need to produce Python code. Input reading: first line: n, v. Then n lines each with a_i, b_i.
Implementation:
def solve():
import sys
input = sys.stdin.readline
n, v = map(int, input().split())
max_day = 0
ripe = [0] * (3002) # index 0 unused, use 1..3001 maybe; but a_i up to 3000, so we need up to 3001.
# Actually a_i up to 3000, so max_day = max(a_i) + 1 <= 3001.
for _ in range(n):
a, b = map(int, input().split())
if a > max_day: max_day = a
ripe[a] += b
# we process days from 1 to max_day+1
ans = 0
carry = 0
for day in range(1, max_day+2): # inclusive of max_day+1
capacity = v
# collect from carry
take1 = min(carry, capacity)
ans += take1
capacity -= take1
# collect from today's ripe
take2 = min(ripe[day], capacity)
ans += take2
# leftover from today becomes carry
carry = ripe[day] - take2
# note: carry from previous day that wasn't taken is already discarded (since we used min(carry,capacity) and we didn't store leftover carry)
print(ans)
But careful: In loop, after we take from carry, we reduce capacity and use the remainder to take from ripe[day]. Then we set carry to ripe[day] - take2. But we need to also consider that we might have leftover from carry if carry > capacity? Actually we took take1 = min(carry, capacity). So if carry > capacity, we used all capacity on carry and take1 = capacity, capacity becomes 0, we take no ripe. Then leftover from carry (carry - capacity) is wasted. We don't need to store it. So after that, we set carry = ripe[day] - take2. That's correct. But we need to ensure we don't keep old carry. So we can simply: carry = max(0, carry - capacity) ??? Actually we compute take1 = min(carry, capacity). Then we could update: carry = carry - take1. But then we also need to add ripe[day] leftovers. So we could:
taken_from_carry = min(carry, capacity)
ans += taken_from_carry
capacity -= taken_from_carry
carry -= taken_from_carry
# then from ripe[day]
take_fresh = min(ripe[day], capacity)
ans += take_fresh
carry += ripe[day] - take_fresh # because carry is now leftover from previous day after taking? Actually we subtracted taken_from_carry, so carry now is leftover from previous day that we didn't take (which is wasted, but we might discard it). However, if we discard, we just set carry = ripe[day] - take_fresh. But careful: What if there is leftover from previous day that we didn't take? It is lost, so we should not add it to tomorrow. So we should set carry = ripe[day] - take_fresh, ignoring any leftover from previous day. So my initial version is fine.
But let's test a scenario where carry > capacity and we only take part of carry. Example: day d: carry = 5, v = 3. Then we take 3 from carry, ans+=3, capacity=0, take2=0. Then carry for next day should be ripe[d] - 0, because leftover from previous day's carry (2) is wasted. So we set carry = ripe[d] - 0. That's correct.
If we did: take1 = min(carry, v); then ans+=take1; capacity=v-take1; then take2 = min(ripe[day], capacity); ans+=take2; then carry = ripe[day] - take2; That discards old carry. Good.
But what about case where we have ripe[day] and we also have leftover from previous day that we didn't take? It's lost, correct.
Thus algorithm is fine.
Potential pitfalls:
-
The array size: Since day goes up to max_day+1, and max_day can be up to 3000, so max_day+1=3001. If we allocate size 3002, indexing ripe[3001] is fine.
-
However, if a_i can be 3000, then we need ripe[3000] and also day 3001. We might need to also handle if there are no trees with max_day+1, we set ripe[day] = 0 for that day. So we need to ensure our ripe list length is at least max_day+2 (since we use index up to max_day+1). So we can set size = max_day+2, or just a large constant like 3005.
-
But we also need to consider days beyond max_day+1? No, because fruits only last two days. After max_day+1, no fruits are available (since any fruit from day max_day would have expired after day max_day+1). So we can stop at max_day+1.
-
Also, we might need to consider that there could be multiple trees with same a_i; we sum them.
Thus solution is straightforward.
We should also consider integer overflow? Not in Python.
Now the problem statement: Input includes n and v (1 ≤ n, v ≤ 3000). Then n lines with ai and bi (1 ≤ ai, bi ≤ 3000). Output maximum fruit.
We'll produce final answer.
Now, test with more cases:
Case: n=1, v=5, tree: (1,10). Then day1: capacity 5, take1=0, take2=min(10,5)=5, ans=5, carry=5. day2: capacity 5, take1=min(5,5)=5, ans=10, carry=0. total=10.
Case: n=1, v=5, tree: (1,3). Day1: take2=3, ans=3, carry=0. Day2: take1=0, ans still 3. That's fine.
Case: n=2, v=2, trees: (1,10), (2,10). Expect: day1: take 2 from ripe1 => ans=2, carry=8. day2: capacity 2, take1=min(8,2)=2, ans=4, cap=0, carry=ripe2 -0 =10? Wait ripe2 is 10, take2=0, so carry=10. day3: capacity 2, take1=min(10,2)=2, ans=6, cap=0, carry=0. So total 6? But we have total fruit 20. Could we do better? Let's think: Day1: take 2 (max). Day2: we could take 2 from carry (from day1) and 0 from day2, or maybe take 1 from carry and 1 from day2? But if we take 1 from carry and 1 from day2, then carry after day2 = leftover from day2 = 9 (since ripe2=10, took 1). Then day3: capacity 2, take from carry (9) => 2, total = day1(2) + day2(2) + day3(2) = 6? Actually if day2: take1=1 from carry, cap=1 left, take2=1 from ripe2, ans+=2 (total 4), leftover carry from day1? Actually careful: day1 carry=8. If day2 we take 1 from carry, we still have 7 from carry leftover? But the carry at start of day2 is 8 (from day1). If we only take 1 from carry, we have capacity left =1. We take 1 from ripe2. Then after day2, we have leftover from carry? But the leftover from carry after taking is 7, but those are from day1 and they expire after day2, so they are lost. So we can't keep them. The algorithm always discards leftover carry after day2 because they are from day1. So if we only take 1 from carry, we lose 7. Then after day2, we have ripe2 leftover = 9 (since we took 1). That becomes carry for day3. So day3 we take min(9,2)=2. Total = day1:2, day2:2, day3:2 =6. But could we get more? Maybe if day1 we take less? Suppose day1 we take 2 (max) cannot take less because we want maximize? Actually we could choose to take less on day1 to save capacity? But capacity is per day, we cannot store unused capacity. So we must use at most v each day. If we take less than v on a day, we waste capacity. So taking less cannot increase total because we are not collecting fruit that could be collected later, but we might leave fruit that would otherwise be lost? But if we take less on day1, we have more ripe1 leftover to day2. But on day2 we also have ripe2, so we might be able to collect more? Let's simulate: day1: take only 1 from ripe1. Then ans=1, carry=9. day2: capacity 2. Take from carry: min(9,2)=2, capacity=0, take2=0, ans=3, carry=ripe2=10 (since take2=0). day3: capacity 2, take1=min(10,2)=2, ans=5. Total=5 <6. So worse.
What if day1: take 2; day2: take 0 from carry, 2 from ripe2? Then day1 ans=2, carry=8. day2: take1=0, capacity=2, take2=2 from ripe2, ans=4, carry = 8 (leftover from day1) + (10-2=8) ??? Actually careful: The carry after day2 is only from ripe2 - take2 = 8, but what about the 8 leftover from day1? It is not carried over; it is lost because it's from day1 and day2 is the last day for them. So we lose 8 from day1. So ans total=2+2=4; plus day3: capacity 2, take1 from carry (8) => 2, ans=6. Same 6.
What about day2: take 2 from carry, 0 from ripe2 gives same 6. So maximum is 6. So greedy yields 6, which seems optimal. Since total fruit 20, but capacity 2 per day, and total days of availability: fruits from day1 available on days1,2; from day2 available on days2,3. So we have 3 days with capacity 2 each, max possible = 6. Actually we could also consider day0? No. So 6 is maximum.
Now test with overlapping days: n=3, v=3, trees: (1,5), (1,5), (2,5). Then ripe1=10, ripe2=5. Day1: capacity3, take2=3 from ripe1, ans=3, carry=7. Day2: capacity3, take1=min(7,3)=3, ans=6, cap=0, take2=0, carry=ripe2=5. Day3: capacity3, take1=min(5,3)=3, ans=9. Total=9. But total fruit=15, but maybe could collect more? Let's see: We could on day1 take 3, day2: maybe take from carry 2 and from ripe2 1? That yields day1:3, day2:2+1=3, day3: capacity3, we can take from carry (ripe2 leftover 4) => 3, total 9. If we on day1 take 3, day2 take 3 from carry, day3 take 3 from ripe2 leftovers (5) total 9. If we take less on day1, say take 2, then day1:2, carry=8. Day2: capacity3, take from carry 3, cap0, take2=0, carry=5, day3: take 3, total=8. So 9 is max. So greedy works.
Now test with scenario where some trees have a_i large, but we need to consider days in between with no fruits. That's fine.
Thus solution is correct.
Now for the code, we should compute max_day from input. Could also set fixed upper bound like 3000+1.
Edge: If there are no trees? But n>=1.
Now final answer.
Potential pitfalls: Input lines may have extra spaces. We'll use split.
Complexity O(n + maxA). Since maxA up to 3000, it's fine.
Now provide final answer.
But we need to be careful about the array indexing. Let's implement with list size max_day+2 to be safe.
Implementation:
def solve():
import sys
input = sys.stdin.readline
n, v = map(int, input().split())
ripe = [0] * (3000 + 2 + 1) # Actually a_i max 3000, so we need up to 3001. Use 3002.
max_day = 0
for _ in range(n):
a, b = map(int, input().split())
ripe[a] += b
if a > max_day:
max_day = a
ans = 0
carry = 0
for day in range(1, max_day+2): # inclusive of max_day+1
capacity = v
# take from carry (yesterday's leftovers)
take = min(carry, capacity)
ans += take
capacity -= take
# take from today's ripe
take2 = min(ripe[day], capacity)
ans += take2
# set carry for next day as leftover from today's ripe
carry = ripe[day] - take2
# note: the leftover from carry (if any) is discarded
print(ans)
But note: In the for loop, if day exceeds the allocated array size? We allocated length maybe 3002, but if max_day is 3000, then max_day+2 = 3002, and we index ripe[3001] (since day goes up to max_day+1 = 3001). Our array length should be at least 3002 (index 3001). If we set length = 3002 (i.e., [0] * 3002), indexes go from 0 to 3001 inclusive. So it's okay.
Alternatively, we could allocate size max_day+3 to be safe.
Now finalize.
But I'd like to double-check the correctness of the greedy with a more complicated scenario to ensure there is no subtle bug. For example, consider multiple days and overlapping. The greedy algorithm is known to be optimal for such problems (like Codeforces problem 349B? Actually it's from Codeforces Round #???). So it's fine.
Now we can output the answer.
But I recall a similar problem "Fruits" from Codeforces 374A? Not sure.
Let's think about potential alternative: some fruits may ripen on day 0? No, a_i>=1.
Thus final answer.
Now we can produce final response with explanation and code.
Given the instruction: "You are an AI assistant that solves competitive programming problems. Provide a final solution with explanation and code." So we should write a detailed explanation of the greedy strategy and then present the Python solution.
Thus answer: We'll describe the algorithm, prove greedy optimality, and give code.
We should also mention that we use arrays of size 3002 or dynamic.
Now produce final answer. ### Solution Explanation
We have n trees, each with a ripening day a_i and number of fruits b_i. Fruits can be collected only on day a_i or a_i + 1. Each day Valera can collect at most v fruits. The goal is to maximize the total collected fruits.
This is a typical problem that can be solved greedily by processing the days in increasing order. For each day we first collect as many fruits as possible that are about to become unavailable (i.e., those that ripened on the previous day and were not collected), because if we miss them today they will be lost forever. After that, if there is still capacity, we collect from the fruits that ripened today (they can still be taken tomorrow if needed).
Formal Greedy
Let ripe[d] be the total number of fruits that ripen exactly on day d (sum of b_i over trees with a_i = d).
Let max_day = max(a_i). Since fruits from day max_day are available also on max_day + 1, we need to consider days 1 … max_day+1.
We maintain a variable carry – the number of fruits that ripened on the previous day and are still available (i.e., not collected on their first day). Initially carry = 0.
We also keep the total answer ans = 0.
For each day d from 1 to max_day+1:
- Set
capacity = v.
- Take from
carry: take1 = min(carry, capacity). Update ans += take1, capacity -= take1.
- Take from today’s ripe fruits:
take2 = min(ripe[d], capacity). Update ans += take2.
- The fruits that ripened today and were not taken become the
carry for the next day: carry = ripe[d] - take2.
(Any leftover from the old carry that was not taken is discarded because it is now expired.)
After processing all days, ans is the maximum possible number of collected fruits.
Why Greedy Works
The key observation is that on any day, fruits that are in carry (i.e., from the previous day) must be collected today or they are lost forever. Fruits that ripened today can still be collected tomorrow. Therefore, it is always optimal to use the day’s capacity first on the carry fruits, and only then on today’s fresh fruits. Any other choice would risk losing the carry fruits without gaining anything, because the fresh fruits can be postponed.
This greedy strategy is essentially a simulation of a maximum flow in a simple bipartite network, and it is provably optimal.
Complexity
- Building
ripe array: O(n).
- Iterating over days: at most
max_day+1 ≤ 3001 days → constant time per day.
Overall time O(n + max_day) which is easily within limits since n, a_i ≤ 3000.
Memory: O(max_day).
Code (Python)
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
v = int(next(it))
# Maximum possible day is 3000, we need up to 3001.
MAX = 3001
ripe = [0] * (MAX + 2) # index 1..MAX+1
max_day = 0
for _ in range(n):
a = int(next(it))
b = int(next(it))
ripe[a] += b
if a > max_day:
max_day = a
ans = 0
carry = 0
for day in range(1, max_day + 2): # include the extra day
capacity = v
# first take from yesterday's leftovers
take = min(carry, capacity)
ans += take
capacity -= take
# then take from today's newly ripe fruits
take = min(ripe[day], capacity)
ans += take
# leftovers from today become carry for tomorrow
carry = ripe[day] - take
# any leftover from previous carry is discarded (expired)
print(ans)
if __name__ == "__main__":
solve()
This code reads all input at once for speed, computes the ripe sums, then runs the greedy loop and prints the result. It handles the edge cases correctly and runs very fast.