## 1. Abridged problem statement

There are N adjacent rivers (no gaps), flowing north with speeds v_i and widths w_i. A geologist starts at the left bank of the westernmost river and must end at the right bank of the easternmost river within total time T.

He swims at speed u relative to water; his ground velocity equals (his swim vector) + (river current (0, v_i)).

Choose how long t_i to spend in each river (and implicitly the swimming direction) to maximize the straight-line distance from the start when he reaches the far right bank. Output -1 if impossible, otherwise output the maximum distance and the optimal times t_1, ..., t_N.

---

## 2. Detailed editorial

### East displacement is fixed

To reach the right bank of the last river, total east movement must be W = sum w_i regardless of path. Therefore maximizing the final distance sqrt(W^2 + Y^2) is equivalent to maximizing total north displacement Y.

### Per river, constant direction is optimal

For a fixed crossing time t_i, changing direction mid-river can only decrease displacement (by a convexity/averaging argument). So we assume one constant swim direction per river.

### Crossing constraint and feasibility

In river i, the current has no east component, so the east swim component must be s_x_i = w_i / t_i. Since |s_x_i| <= u, we need t_i >= w_i / u. Feasibility requires sum(w_i / u) <= T; otherwise output -1.

### Given time t_i, best north contribution

With s_x_i = w_i / t_i fixed, the maximum north swim component is s_y_i = sqrt(u^2 - (w_i/t_i)^2). The north displacement contribution from river i becomes:

```
g_i(t_i) = v_i * t_i + sqrt((u * t_i)^2 - w_i^2)
```

We maximize sum g_i(t_i) subject to sum t_i = T, t_i >= w_i/u. Since extra time always increases Y, the optimum uses all T seconds.

### KKT/Lagrange multiplier gives a single lambda

For t > w_i/u, g_i(t) is concave; its derivative strictly decreases:

```
g_i'(t) = v_i + (u^2 * t) / sqrt((u*t)^2 - w_i^2)
```

The derivative blows up as t approaches w_i/u from above and approaches v_i + u as t -> infinity. Because the derivative is infinite at the lower bound, the inequality constraints t_i >= w_i/u are never active at the optimum. By KKT, there exists a single lambda such that g_i'(t_i) = lambda for all i.

### Closed-form solution for t_i(lambda)

Setting a = lambda - v_i and solving g_i'(t) = lambda:

```
t_i(lambda) = w_i * a / (u * sqrt(a^2 - u^2))    (valid when a > u, i.e. lambda > v_i + u)
```

So lambda must be greater than max_i(v_i + u). Since sum t_i(lambda) is monotonically decreasing in lambda, we binary search lambda to satisfy sum t_i(lambda) = T.

### Algorithm

1. Check feasibility: if sum(w_i / u) > T, output -1.
2. Binary search lambda in (max_i(v_i + u), +inf): for each lambda compute t_i = w_i * (lambda - v_i) / (u * sqrt((lambda - v_i)^2 - u^2)), then sum them. Increase lambda if sum > T, decrease if sum < T. About 200 iterations suffice.
3. Compute W = sum w_i, Y = sum(v_i * t_i + sqrt((u * t_i)^2 - w_i^2)), distance = sqrt(W^2 + Y^2).
4. Output distance and times.

Complexity: O(N * I) with I ~= 200 iterations; N <= 50 is trivial.

---

## 3. C++ Solution

```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int N;
double u, T;
vector<double> w, v;

void read() {
    cin >> N >> u >> T;
    w.resize(N);
    v.resize(N);
    for(int i = 0; i < N; i++) {
        cin >> w[i] >> v[i];
    }
}

void solve() {
    // We can think of this as an optimization problem. Let t[1], ..., t[n] be
    // the times we spend at each river. Clearly T = t[1] + ... + t[n], because
    // there is no point in wasting time waiting. Also, we know the smallest
    // amount of time t[i] >= w[i] / u. For a given t[i], we can always show
    // that it's optimal to swim in a constant direction vector (x[i], y[i])
    // with a fairly simple convexity argument (if we change directions at some
    // point, we can consider the average between the two vectors for the same
    // amount of time). The constraint is that u^2 = x[i]^2 + (y[i] + v[i])^2.
    //
    // For fixed t[i] we must have x[i] = w[i] / t[i] (exact crossing). To
    // maximize total northward displacement we take the root:
    //
    //     y[i] = -v[i] + sqrt(u^2 - x[i]^2)
    //
    // The total eastward displacement W = sum w[i] is fixed. The total
    // northward displacement becomes:
    //
    //     Y = sum g_i(t[i])
    //       = sum y[i]*t[i]
    //       = sum v[i]*t[i] + sum sqrt((u*t[i])^2 - w[i]^2).
    //
    // We therefore need to choose t[1]..t[n] (t[i] >= w[i]/u, sum t[i] = t)
    // that maximize Y. The final answer distance is sqrt(W*W + Y*Y). If sum
    // (w[i]/u) > t it is impossible (-1). Otherwise the optimum always uses
    // exactly t seconds (extra time always gives extra drift).
    //
    // To optimize this, we can do a gradient descent like approach, but we can
    // also do Lagrange multipliers. Being more precise, the equality constraint
    // is a single one in T = t[1] + ... + t[n], so the partial derivative of
    // this is 1 for each t[i]. However, we have the inequality constraints t[i]
    // >= w[i] / u for which we also need a Lagrange multiplier, which we denote
    // my \mu_i. By KKT, we simply know that g_i'(t) = \lambda - \mu_i. However,
    // g_i(t) is concave, or g_i'(t) is strictly decreasing and t -> w[i]/u
    // has g_i'(t) -> +inf, meaning the optimum we always has t[i] > w[i]/u
    // because g_i'(t) is undefined before w[i]/u. In other words, any t[:] \in
    // R^n we find will have t[i] > w[i]/u simply because the g_i'(t) term
    // requires this. This means that \mu_i will be inactive and we can set
    // \mu_i = 0. Therefore, all g_i'(t) = \lambda for the same \lambda. The
    // derivative is:
    //
    //     g_i'(t) = v[i] + (u*u*t) / sqrt((u*t)^2 - w[i]*w[i])
    //
    // For a candidate \lambda we solve for each t[i] in closed form:
    //
    //     a = \lambda - v[i]
    //     t[i] = w[i] * a / (u * sqrt(a*a - u*u))     (valid when a > u)
    //
    // We binary-search \lambda (approx 100-200 iterations suffice for 1e-12
    // precision) until sum t[i] equals the given t (larger \lambda makes every
    // t[i] smaller). This gives us both the optimal Y and the t[1], ..., t[n]
    // (and so answer to the problem). It works in O(N log eps^-1), ignoring the
    // complexity of explicitly solving the t[i] that requires computing a
    // square root.

    double min_time = 0.0;
    for(int i = 0; i < N; i++) {
        min_time += w[i] / u;
    }

    if(min_time > T + 1e-12) {
        cout << -1 << endl;
        return;
    }

    double max_asympt = 0.0;
    for(int i = 0; i < N; i++) {
        max_asympt = max(max_asympt, v[i] + u);
    }

    auto compute_times = [&](double lam) -> vector<double> {
        vector<double> res(N);
        for(int i = 0; i < N; i++) {
            double a = lam - v[i];
            double sqrt_term = sqrt(a * a - u * u);
            res[i] = w[i] * a / (u * sqrt_term);
        }
        return res;
    };

    double low = max_asympt + 1e-9;
    double high = 1e18;
    for(int iter = 0; iter < 200; iter++) {
        double mid = (low + high) / 2.0;
        auto ts = compute_times(mid);
        double sumt = 0.0;
        for(double t: ts) {
            sumt += t;
        }
        if(sumt > T) {
            low = mid;
        } else {
            high = mid;
        }
    }

    double lambda_val = (low + high) / 2.0;
    auto times = compute_times(lambda_val);

    double Y = 0.0;
    double W = 0.0;
    for(int i = 0; i < N; i++) {
        W += w[i];
        double ti = times[i];
        Y += v[i] * ti + sqrt(u * ti * ti * u - w[i] * w[i]);
    }
    double dist = sqrt(W * W + Y * Y);

    cout << fixed << setprecision(10) << dist << endl;
    cout << fixed << setprecision(10);
    for(int i = 0; i < N; i++) {
        if(i > 0) {
            cout << ' ';
        }
        cout << times[i];
    }
    cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        solve();
    }

    return 0;
}
```

---

## 4. Python solution with detailed comments

```python
import math
import sys

def solve() -> None:
    data = sys.stdin.read().strip().split()
    if not data:
        return
    it = iter(data)
    N = int(next(it))
    u = float(next(it))
    T = float(next(it))

    w = [0.0] * N
    v = [0.0] * N
    for i in range(N):
        w[i] = float(next(it))
        v[i] = float(next(it))

    # Minimal time to cross all rivers: each river i needs at least w_i/u seconds,
    # because your east component in still water cannot exceed u.
    min_time = sum(wi / u for wi in w)
    if min_time > T + 1e-12:
        print(-1)
        return

    # Lambda must satisfy lambda > v_i + u for all i, otherwise t_i(lambda) is undefined.
    max_asympt = max(vi + u for vi in v)

    # Compute the vector of times t_i given lambda using the derived closed-form:
    # a = lambda - v_i
    # t_i = w_i * a / (u * sqrt(a^2 - u^2))
    def compute_times(lam: float):
        res = [0.0] * N
        uu = u * u
        for i in range(N):
            a = lam - v[i]
            # a must be > u, ensured by our binary search lower bound
            sqrt_term = math.sqrt(a * a - uu)
            res[i] = w[i] * a / (u * sqrt_term)
        return res

    # Binary search lambda so that sum t_i == T.
    low = max_asympt + 1e-9
    high = 1e18

    for _ in range(200):
        mid = (low + high) / 2.0
        ts = compute_times(mid)
        sumt = sum(ts)

        # sumt decreases as lambda increases.
        if sumt > T:
            low = mid
        else:
            high = mid

    lam = (low + high) / 2.0
    times = compute_times(lam)

    # Compute W (total east) and Y (total north), then distance.
    W = sum(w)
    Y = 0.0
    for i in range(N):
        ti = times[i]
        # north displacement in river i:
        # v_i * t_i + sqrt((u*t_i)^2 - w_i^2)
        Y += v[i] * ti + math.sqrt((u * ti) * (u * ti) - w[i] * w[i])

    dist = math.hypot(W, Y)  # sqrt(W^2 + Y^2)

    # Output formatting
    print(f"{dist:.10f}")
    print(" ".join(f"{ti:.10f}" for ti in times))

if __name__ == "__main__":
    solve()
```

---

## 5. Compressed editorial

- East displacement W = sum w_i is fixed; maximize north displacement Y = sum g_i(t_i) where g_i(t) = v_i*t + sqrt((u*t)^2 - w_i^2).
- Feasibility: if sum(w_i/u) > T, output -1.
- g_i is concave; KKT gives a single lambda such that g_i'(t_i) = lambda for all i.
- Derivative: g_i'(t) = v_i + (u^2 * t) / sqrt((u*t)^2 - w_i^2).
- Solve for t_i(lambda) in closed form: t_i = w_i*(lambda - v_i) / (u*sqrt((lambda - v_i)^2 - u^2)), valid for lambda > v_i + u.
- Binary search lambda (200 iterations) so sum t_i(lambda) = T. Compute dist = sqrt(W^2 + Y^2). Output dist and times.
