algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
2 shown · 0/261 solved

Minimum Total Travel Cost

SilverCommunity Beta
Asked atConcentric AI
Solve problem →
2000ms256MBAdded Aug 17, 2026

You are given three integer arrays of the same length: counts, xs, and ys. For each index i, there are counts[i] people located at the point (xs[i], ys[i]) on a grid.

Choose a meeting point (x, y) (any pair of integers) that minimizes the total travel cost, where each person's cost is the Manhattan distance from their location to the meeting point:

totalCost(x, y) = Σ counts[i] * (|x - xs[i]| + |y - ys[i]|)

Return the minimum possible total cost.

Examples

Example 1

Input: counts = [1, 2], xs = [1, 3], ys = [1, 3]
Output: 4
Explanation: Meeting at (3, 3): the 1 person at (1, 1) travels |3-1| + |3-1| = 4,
and the 2 people at (3, 3) travel 0. Total = 1*4 + 2*0 = 4. No point does better.

Example 2

Input: counts = [5], xs = [-7], ys = [9]
Output: 0
Explanation: Meeting at (-7, 9) costs nothing.

Constraints

  • 1≤n≤1051 \leq n \leq 10^51≤n≤105 where n=n =n= counts.length === xs.length === ys.length
  • 1≤1 \leq1≤ counts[i] ≤104\leq 10^4≤104
  • −106≤-10^6 \leq−106≤ xs[i], ys[i] ≤106\leq 10^6≤106

Details

Solved by5 people
Time limit2000ms
Memory256MB
AddedAug 17, 20263 weeks ago