import math

# --- problem setup ---
A = (0.0, 0.0)
B = (100.0, 0.0)
L = [25.0, 15.0, 5.0, -5.0, -15.0, -25.0]       # marsh‑boundary offsets
speeds = [10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 10.0]   # segment speeds

def total_time(x_vec):
    # build crossing points
    pts = [A]
    for xi, Li in zip(x_vec, L):
        yi = xi - 50.0 + Li * math.sqrt(2)
        pts.append((xi, yi))
    pts.append(B)
    # sum segment times
    t = 0.0
    for i in range(len(speeds)):
        x0, y0 = pts[i]
        x1, y1 = pts[i+1]
        d = math.hypot(x1 - x0, y1 - y0)
        t += d / speeds[i]
    return t

# initial guess: direct route intersections
x = [50.0 - Li * math.sqrt(2) for Li in L]

# gradient descent parameters
alpha = 1.0
eps = 1e-6
tol = 1e-8
max_iters = 10000

def numerical_grad(f, x):
    grad = [0.0]*len(x)
    for i in range(len(x)):
        x_eps_p = x.copy(); x_eps_p[i] += eps
        x_eps_m = x.copy(); x_eps_m[i] -= eps
        grad[i] = (f(x_eps_p) - f(x_eps_m)) / (2*eps)
    return grad

for _ in range(max_iters):
    g = numerical_grad(total_time, x)
    # compute new candidate
    x_new = [xi - alpha*gi for xi, gi in zip(x, g)]
    if total_time(x_new) < total_time(x):
        x = x_new
    else:
        alpha *= 0.5
    if math.sqrt(sum(gi*gi for gi in g)) < tol:
        break

opt_time = total_time(x)
print(f"Optimal time: {opt_time:.10f} days")