<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

114. Telecasting station

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


Every city in Berland is situated on Ox axis. The government of the country decided to build new telecasting station. After many experiments Berland scientists came to a conclusion that in any city citizens displeasure is equal to product of citizens amount in it by distance between city and TV-station. Find such point on Ox axis for station so that sum of displeasures of all cities is minimal.


Input

Input begins from line with integer positive number N (0<N<15000) – amount of cities in Berland. Following N pairs (X, P) describes cities (0<X, P<50000), where X is a coordinate of city and P is an amount of citizens. All numbers separated by whitespace(s).


Output

Write the best position for TV-station with accuracy 10-5.


Sample Input

4
1 3
2 1
5 2
6 2
Sample Output

3.00000
Author	: Michael R. Mirzayanov
Resource	: PhTL #1 Training Contests
Date	: Fall 2001

<|response|>
1. Abridged Problem Statement
You have N cities on the real line. City i is at coordinate xᵢ and has population pᵢ. You want to choose a point p on the line (where to build a TV-station) so as to minimize the total displeasure
  F(p) = Σ₁ⁿ pᵢ·|xᵢ − p|.
Output any optimal p with absolute error ≤ 10⁻⁵.

2. Key Observations
• F(p) is convex and piecewise linear in p.
• The slope of F(p) jumps at each city coordinate xᵢ.
• For p not equal to any xᵢ, the derivative is
  F′(p) = (sum of weights to the left of p) − (sum of weights to the right of p).
• The minimum occurs when F′(p) crosses zero, i.e. when the total weight on each side is at most half of the grand total. Such a point p is called a weighted median.
• Because F is convex, we can also locate its minimum by ternary search on the interval [min xᵢ, max xᵢ], which is exactly what the reference solution does.

3. Full Solution Approach
1. Read N and the list of pairs (xᵢ, pᵢ).
2. Set the search interval [l, r] = [min xᵢ, max xᵢ].
3. Repeat ~70 times: pick m1 = l + (r-l)/3 and m2 = r - (r-l)/3, evaluate F(m1) and F(m2) in O(N), and discard the side that cannot contain the minimum (if F(m1) ≤ F(m2), set r = m2; otherwise l = m1).
4. After enough iterations, the interval is narrower than 10⁻⁵; print r with 5 decimal places.
Overall time complexity: O(N · iterations).

(An exact O(N log N) alternative sorts the cities by coordinate and outputs the first xⱼ at which the prefix population reaches W/2 — the weighted median.)

4. C++ Implementation
```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;
vector<int> x, a;

void read() {
    cin >> n;
    x.resize(n);
    a.resize(n);
    for(int i = 0; i < n; i++) {
        cin >> x[i] >> a[i];
    }
}

double cost(double p) {
    double res = 0;
    for(int i = 0; i < n; i++) {
        res += a[i] * abs(x[i] - p);
    }

    return res;
}

void solve() {
    // The total displeasure sum_i a[i] * |x[i] - p| is a convex piecewise
    // linear function of the station position p, so its minimum is found by
    // ternary search on [min x, max x]. (The optimum is the weighted median,
    // but ternary search converges to it within the required precision.)

    double l = *min_element(x.begin(), x.end());
    double r = *max_element(x.begin(), x.end());
    for(int steps = 0; steps < 70; steps++) {
        double m1 = l + (r - l) / 3;
        double m2 = r - (r - l) / 3;
        if(cost(m1) <= cost(m2)) {
            r = m2;
        } else {
            l = m1;
        }
    }

    cout << setprecision(5) << fixed << r << '\n';
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

5. Python Implementation with Detailed Comments
```python
import sys

def main():
    data = sys.stdin.read().split()
    it = iter(data)
    N = int(next(it))

    # Read and store (xᵢ, pᵢ)
    cities = []
    for _ in range(N):
        x = float(next(it))
        p = float(next(it))
        cities.append((x, p))

    # Sort by x-coordinate
    cities.sort(key=lambda cp: cp[0])

    # Total population
    total = sum(p for _, p in cities)
    half = total / 2.0

    # Find weighted median
    prefix = 0.0
    answer = cities[-1][0]
    for x, p in cities:
        prefix += p
        if prefix >= half:
            # x is the optimal station location
            answer = x
            break

    # Print with 5 decimal places
    print(f"{answer:.5f}")

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