A traveler starts at city 0 and must reach city n - 1. The array cities gives the money earned or spent when visiting each city. From city i, the traveler may move forward by 1 or by p positions, where p is a prime whose decimal representation ends in 3. Add the value of every visited city, including the starting and ending cities, to the journey total.
Return the maximum possible journey total. Cities skipped by a jump contribute nothing, and a jump must remain within the array.
Function Signature
def optimal_journey_total(cities: list[int]) -> int:
Constraints
-
1 <= len(cities) <= 10000
.
-
-10000 <= cities[i] <= 10000
and
cities[0] == 0
.
-
Movement is strictly forward. Negative intermediate totals are permitted.
-
All totals fit in signed 32-bit integers.
Examples
Input: cities = [0, 100, 200, -500, -100, -150, -50]
Output: 100
Visit indices 0, 1, 2, 5, 6, collecting 0 + 100 + 200 - 150 - 50 = 100. The only permitted prime jump within this array is 3.
Input: cities = [0, -5, -2, 7]
Output: 7
A jump of length 3 reaches the final city directly.
Input: cities = [0]
Output: 0
The starting city is already the destination.