There are n beacons on a line, the i-th at position x_i with signal strength y_i. The positions are given in strictly increasing order, and each consecutive gap is at most k (so x_{i+1} - x_i <= k).
For any pair of beacons i < j with x_j - x_i <= k, their combined signal is y_i + y_j + (x_j - x_i). Report the maximum combined signal over all such pairs. Because consecutive positions differ by at most k, at least one valid pair always exists.
Line 1: two integers n and k.
Next n lines: two integers x_i and y_i, with x strictly increasing and consecutive gaps at most k.
A single integer: the maximum combined signal over all valid pairs.
Example 1
Input
4 3 1 3 2 0 4 5 5 1
Expected
11
Explanation
The pair at positions 1 and 4 gives 3 + 5 + (4 - 1) = 11, which beats every other pair within distance 3.
Example 2
Input
2 5 0 4 2 6
Expected
12
Explanation
The only pair is within distance 5, giving 4 + 6 + (2 - 0) = 12.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →