You are given two integer arrays times and effectiveness of equal length n, where times[i] is the minute at which the i-th ad can be shown and effectiveness[i] is its effectiveness value. You are also given an integer m representing the length of the time window: ads may only be shown at minutes in the range [0, m].
You must select a subset of ads to show such that any two selected ads are at least 4 minutes apart (the difference between the times of any two selected ads must be at least 4).
Return the maximum possible sum of effectiveness values of the selected ads.
Function Signature
def max_effectiveness(times: list[int], effectiveness: list[int], m: int) -> int:
Rules
-
times[i]
and
effectiveness[i]
describe the same ad. The arrays are not sorted, and several ads can share a minute.
-
Two ads at minutes
s
and
t
can both be selected only when
abs(s - t) >= 4
. A gap of exactly 4 minutes is allowed, and two ads at the same minute can never both be selected.
-
Every time lies in
[0, m]
(see Constraints), so every ad is eligible.
Constraints
-
1 <= n <= 10^5
-
0 <= times[i] <= m
-
1 <= m <= 10^9
-
1 <= effectiveness[i] <= 10^4
-
len(times) == len(effectiveness)
Examples
Input: times = [1, 5, 9, 3, 10], effectiveness = [5, 6, 8, 4, 3], m = 15
Output: 19
Sorted by time, the ads are (1, 5), (3, 4), (5, 6), (9, 8), (10, 3). Picking minutes 1, 5 and 9 gives 5 + 6 + 8 = 19, with gaps of exactly 4. Minute 3 is too close to minutes 1 and 5, and minute 10 is too close to minute 9; swapping 10 in for 9 gives only 14.
Input: times = [0, 2, 4, 6], effectiveness = [10, 20, 30, 40], m = 10
Output: 60
Three ads would need a span of at least 8 minutes, so at most two fit. The valid pairs are minutes 0 and 4 (40), 0 and 6 (50), and 2 and 6 (60), and the best is 60.
Input: times = [7, 7, 3], effectiveness = [2, 9, 5], m = 7
Output: 14
The two ads at minute 7 cannot both be shown. Minute 3 is exactly 4 minutes before minute 7, so the ad at minute 3 pairs with the better ad at minute 7 for 5 + 9 = 14.