## 1) Abridged problem statement

There are **N** adjacent rivers flowing north with speeds \(v_i\) and widths \(w_i\). A geologist starts on the **left bank** of the westernmost river and must end on the **right bank** of the easternmost river within **total time \(T\)**.  
He can swim with speed **\(u\)** in still water; in river \(i\), his **ground velocity** is the vector sum of his swim velocity (magnitude \(u\)) and the river flow \((0, v_i)\).

Choose how long \(t_i\) to spend crossing each river (and implicitly the swimming direction in that river) to **maximize the straight-line distance** from the start to the final point, subject to:
- He crosses each river completely: total east displacement across river \(i\) equals \(w_i\).
- \(\sum t_i = T\), \(t_i \ge 0\).
Output the maximum distance (or **-1** if impossible), and one optimal sequence \(t_1,\dots,t_N\).

---

## 2) Detailed editorial (solution explanation)

### Geometry and reducing the control strategy
In river \(i\), let the swimmer choose a constant swimming velocity vector (relative to water)
\[
\vec{s}_i = (s_{x,i}, s_{y,i}), \quad \|\vec{s}_i\| = u.
\]
The current adds \((0, v_i)\), so the **ground velocity** is
\[
\vec{g}_i = (s_{x,i},\, s_{y,i}+v_i).
\]

A standard convexity/averaging argument shows: **for fixed crossing time \(t_i\)**, changing direction mid-river cannot improve the final displacement compared to using the average direction for the whole time. Thus, we may assume **constant direction per river**.

### East constraint forces the east ground speed
To cross width \(w_i\) in time \(t_i\), the east ground speed must be
\[
g_{x,i} = \frac{w_i}{t_i}.
\]
But \(g_{x,i} = s_{x,i}\) (current has no east component), so
\[
s_{x,i} = \frac{w_i}{t_i}.
\]
Since \(\|\vec{s}_i\|=u\), we need \(|s_{x,i}| \le u\), i.e.
\[
t_i \ge \frac{w_i}{u}.
\]
So feasibility requires
\[
\sum_{i=1}^N \frac{w_i}{u} \le T.
\]
If not, answer is **-1**.

### For a given \(t_i\), best north component
Given \(s_{x,i}=w_i/t_i\), we choose \(s_{y,i}\) as large as possible:
\[
s_{y,i} = \sqrt{u^2 - \left(\frac{w_i}{t_i}\right)^2}.
\]
(Choose the positive root to maximize northward travel.)

Then the north ground speed is \(g_{y,i} = s_{y,i}+v_i\), so north displacement in river \(i\) is
\[
Y_i(t_i)= (v_i + \sqrt{u^2-(w_i/t_i)^2})\,t_i
= v_i t_i + \sqrt{(u t_i)^2 - w_i^2}.
\]

Total east displacement is fixed:
\[
W = \sum w_i,
\]
and total north displacement is
\[
Y = \sum_{i=1}^N \left( v_i t_i + \sqrt{(u t_i)^2 - w_i^2} \right).
\]
The final distance from the start is
\[
D = \sqrt{W^2 + Y^2}.
\]
Since \(W\) is constant, maximizing \(D\) is equivalent to maximizing \(Y\).

Also, if feasible, it is always optimal to use **all** time \(T\) because each \(Y_i(t)\) is increasing in \(t\) for \(t \ge w_i/u\).

So we solve:
- maximize \( \sum g_i(t_i)\)
- subject to \( \sum t_i = T\), \(t_i \ge w_i/u\),
where
\[
g_i(t)= v_i t + \sqrt{(u t)^2 - w_i^2}.
\]

### Concavity and KKT/Lagrange multiplier condition
For \(t > w_i/u\), \(g_i(t)\) is **concave**, so this is a concave maximization under linear constraints ⇒ unique optimum (up to numerics) and KKT applies.

Derivative:
\[
g_i'(t) = v_i + \frac{u^2 t}{\sqrt{(u t)^2 - w_i^2}}.
\]
Key properties:
- \(g_i'(t)\to +\infty\) as \(t \downarrow w_i/u\),
- \(g_i'(t)\downarrow v_i+u\) as \(t\to\infty\),
- strictly decreasing in \(t\).

Because the derivative blows up at the lower bound, in the optimum we effectively have \(t_i > w_i/u\) (the lower bounds are not “active” in the KKT sense), and there exists a single \(\lambda\) such that:
\[
g_i'(t_i) = \lambda \quad \forall i.
\]

### Solving \(g_i'(t)=\lambda\) in closed form
Set \(a = \lambda - v_i\). Then:
\[
a = \frac{u^2 t}{\sqrt{(u t)^2 - w_i^2}}.
\]
Rearrange (squaring carefully):
\[
\sqrt{(u t)^2 - w_i^2} = \frac{u^2 t}{a}
\]
\[
(u t)^2 - w_i^2 = \frac{u^4 t^2}{a^2}
\]
\[
u^2 t^2\left(1-\frac{u^2}{a^2}\right)= w_i^2
\]
\[
t^2 = \frac{w_i^2 a^2}{u^2(a^2-u^2)}
\]
\[
t = \frac{w_i a}{u\sqrt{a^2-u^2}}.
\]
This requires \(a>u\), i.e.
\[
\lambda > v_i + u.
\]
So \(\lambda\) must be greater than \(\max_i(v_i+u)\).

Define
\[
t_i(\lambda) = \frac{w_i (\lambda - v_i)}{u\sqrt{(\lambda - v_i)^2-u^2}}.
\]
Then \(\sum t_i(\lambda)\) is **monotonically decreasing** in \(\lambda\).  
So we can **binary search** \(\lambda\) to make:
\[
\sum_{i=1}^N t_i(\lambda) = T.
\]

### After finding times, compute answer
Compute:
- \(W=\sum w_i\)
- \(Y=\sum \left(v_i t_i + \sqrt{(u t_i)^2 - w_i^2}\right)\)
- \(D=\sqrt{W^2+Y^2}\)

Complexity: \(O(N \cdot I)\) with \(I\approx 200\) iterations, fine for \(N\le 50\).

---

## 3) Provided C++ solution with detailed line-by-line comments

```cpp
#include <bits/stdc++.h>              // Pulls in almost all standard headers (fast for contests)
using namespace std;

// Convenience: print a pair as "first second"
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

// Convenience: read a pair "first second"
template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

// Convenience: read all elements of a vector
template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

// Convenience: print all elements of a vector separated by spaces
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int N;                 // number of rivers
double u, T;           // swim speed in still water, and total time available
vector<double> w, v;   // widths w[i] and current speeds v[i]

void read() {
    cin >> N >> u >> T;        // read N, u, T (given as integers in statement but stored as double)
    w.resize(N);
    v.resize(N);
    for(int i = 0; i < N; i++) {
        cin >> w[i] >> v[i];   // read river i width and speed
    }
}

void solve() {
    // The long comment in the original code explains the math:
    // reduce to choosing times t[i], derive optimality via Lagrange multipliers/KKT,
    // solve t[i](lambda) in closed-form, binary search lambda.

    // Compute minimal possible total time: for each river, need at least w[i]/u
    // (because east component of swim speed cannot exceed u).
    double min_time = 0.0;
    for(int i = 0; i < N; i++) {
        min_time += w[i] / u;
    }

    // If even with full speed straight east we can't cross all widths in time T => impossible.
    if(min_time > T + 1e-12) {
        cout << -1 << endl;
        return;
    }

    // For the equation g_i'(t)=lambda to have a solution, we need lambda > v[i]+u for all i.
    // So lambda must be > max_i(v[i]+u). We compute that threshold.
    double max_asympt = 0.0;
    for(int i = 0; i < N; i++) {
        max_asympt = max(max_asympt, v[i] + u);
    }

    // Given a lambda, compute the implied optimal time for each river:
    // t[i] = w[i] * a / (u * sqrt(a^2 - u^2)), where a = lambda - v[i].
    auto compute_times = [&](double lam) -> vector<double> {
        vector<double> res(N);
        for(int i = 0; i < N; i++) {
            double a = lam - v[i];                // a = lambda - v_i
            double sqrt_term = sqrt(a * a - u * u); // sqrt(a^2 - u^2), valid since a>u
            res[i] = w[i] * a / (u * sqrt_term);  // closed-form t_i(lambda)
        }
        return res;
    };

    // Binary search for lambda such that sum_i t_i(lambda) == T.
    // Lower bound: just above max_asympt to satisfy a>u strictly.
    double low = max_asympt + 1e-9;
    // Upper bound: a very large number; for huge lambda, times shrink toward min_time.
    double high = 1e18;

    // Iterate enough times to get very high precision.
    for(int iter = 0; iter < 200; iter++) {
        double mid = (low + high) / 2.0;     // candidate lambda
        auto ts = compute_times(mid);        // compute times for this lambda

        double sumt = 0.0;
        for(double t: ts) {
            sumt += t;                       // total time spent crossing all rivers
        }

        // If sumt > T, we need to make times smaller => increase lambda (move low up).
        if(sumt > T) {
            low = mid;
        } else {
            // If sumt <= T, lambda is high enough (times small enough); move high down.
            high = mid;
        }
    }

    // Our lambda approximation and resulting times
    double lambda_val = (low + high) / 2.0;
    auto times = compute_times(lambda_val);

    // Compute total east displacement W and total north displacement Y.
    // For river i:
    // north displacement = v_i * t_i + sqrt((u t_i)^2 - w_i^2).
    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]);
        // Note: u*ti*ti*u == (u^2)*(ti^2) == (u*ti)^2
    }

    // Final distance from start to end point
    double dist = sqrt(W * W + Y * Y);

    // Output distance with 10 decimals
    cout << fixed << setprecision(10) << dist << endl;

    // Output times with 10 decimals, space-separated
    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); // faster I/O
    cin.tie(nullptr);                 // untie cin from cout for speed

    int T = 1;                        // number of test cases (problem has single test)
    // cin >> T;                       // disabled
    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

- Need end on far right bank: total east displacement is fixed \(W=\sum w_i\). Maximize final distance ⇔ maximize total north displacement \(Y\).
- In river \(i\), optimal to swim with constant direction; to cross width \(w_i\) in time \(t_i\), east component is \(w_i/t_i\), requiring \(t_i \ge w_i/u\). If \(\sum w_i/u > T\) ⇒ **-1**.
- Given \(t_i\), maximize north speed by taking
  \[
  s_{y,i}=\sqrt{u^2-(w_i/t_i)^2},
  \]
  so north displacement contribution is
  \[
  g_i(t_i)=v_i t_i + \sqrt{(u t_i)^2-w_i^2}.
  \]
- Maximize \(\sum g_i(t_i)\) subject to \(\sum t_i=T\). \(g_i\) is concave; KKT gives \(g_i'(t_i)=\lambda\) for all \(i\).
- Derivative:
  \[
  g_i'(t)=v_i+\frac{u^2 t}{\sqrt{(u t)^2-w_i^2}}.
  \]
  Solve \(g_i'(t)=\lambda\) explicitly:
  \[
  t_i(\lambda)=\frac{w_i(\lambda-v_i)}{u\sqrt{(\lambda-v_i)^2-u^2}},\quad \lambda>\max(v_i+u).
  \]
- Since \(\sum t_i(\lambda)\) decreases with \(\lambda\), binary search \(\lambda\) so \(\sum t_i=T\).
- Compute \(Y=\sum g_i(t_i)\), answer \(D=\sqrt{W^2+Y^2}\), output \(D\) and the times.