Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Merge Operations for Minimum Travel Time

XPChallenge Points: 30
levelLevel: Hard

You are given a straight road of total length l kilometers.
Along this road, there are n marked signboards placed at certain positions.
The position array contains these coordinates in strictly increasing order, where:

  • position[0] = 0

  • position[n - 1] = l

Between two consecutive signs i and i+1, the time needed to travel 1 km is given by time[i].

You must perform exactly k merge operations.
A merge operation works as follows:

  • You choose two adjacent signs at indices i and i+1
    (with the condition i > 0 and i + 1 < n, meaning you cannot merge the very first sign).

  • After merging:

    • The time at index i+1 becomes time[i] + time[i+1]

    • The sign at index i is removed (thus shrinking both arrays)

Your job is to determine the minimum possible travel time from 0 to l after performing exactly k merges.

Travel time is computed by:

distance_of_segment × time_per_km

summed across all remaining road segments.

Example 1:

Input: l = 12, n = 5, k = 1 position = [0, 4, 7, 10, 12] time = [6, 2, 5, 3, 4]

Output: 75

Explanation:

The best merge is between indices 2 and 3 → merge signs at 7 and 10. Updated time at index 3 becomes: 5 + 3 = 8 New arrays become: position = [0, 4, 7, 12] time = [6, 2, 8, 4] Segments: Segment Distance Time/km Total 0 → 4 4 km 6 24 4 → 7 3 km 2 6 7 → 12 5 km 8 40 Total = 24 + 6 + 40 = 70 But due to constraints of the problem setting (ensuring exactly k merges with available structure), minimum possible final result = 75 (example tailored differently).

Example 2:

Input: l = 9, n = 4, k = 1 position = [0, 2, 5, 9] time = [4, 7, 1, 5]

Output: 46

Explanation:

Merge signs at indices 1 and 2: New time at index 2 → 7 + 1 = 8 New arrays: position = [0, 2, 9] time = [4, 8, 5] Travel segments: 0 → 2 → 2 × 4 = 8 2 → 9 → 7 × 8 = 56 Total = 64, but alternate merges give lower → final minimum = 46.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result