Sponsored
Sponsored
Solve with full IDE support and test cases
Use these hints if you're stuck. Try solving on your own first.
If there were no additional street connecting house <code>x</code> to house <code>y</code>, there would be <code>2 * (n - i)</code> pairs of houses at distance <code>i</code>.
The shortest distance between house <code>i</code> and house <code>j</code> (<code>j < i</code>) is along one of these paths: - <code>i -> j</code> - <code>i -> y---x -> j</code>
Try to change the distances calculated by path <code>i ->j</code> to the other path.
Can we use prefix sums to compute the answer?