def min_total_travel_time(A):
MOD = 10**9 + 7
N = len(A)
# Calculate the total travel time without building any motorways
total_time = A[N-1] - A[0]
# Initialize an array to store the minimum travel time for each city
min_travel_time = [float('inf')] * N
# Traverse the cities from left to right and find the minimum travel time to the easternmost city
for i in range(N):
for j in range(i + 1, N):
min_travel_time[j] = min(min_travel_time[j], A[j] - A[i])
# Initialize an array to store the maximum travel time for each city
max_travel_time = [0] * N
# Traverse the cities from right to left and find the maximum travel time to the easternmost city
for i in range(N - 1, -1, -1):
for j in range(i - 1, -1, -1):
max_travel_time[j] = max(max_travel_time[j], A[i] - A[j])
# Initialize the minimum total travel time
min_total_time = total_time
# Find the minimum total travel time with a motorway
for i in range(1, N - 1):
min_total_time = min(min_total_time, total_time - max_travel_time[i] + min_travel_time[i])
return min_total_time % MOD
# Example usage:
A = [0, 1, 3, 6, 7, 9]
result = min_total_travel_time(A)
print(result)
No comments:
Post a Comment