p114.ans1
======================
3.00000

=================
p114.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;
}

=================
p114.in1
======================
4
1 3
2 1
5 2
6 2

=================
statement.txt
======================
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

=================
