p146.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;
};

const int64_t B = 10000;

int64_t L;
int N;
vector<pair<int64_t, int64_t>> a;

void read() {
    double _L;
    cin >> _L;
    L = (int64_t)(_L * B + 0.5);
    cin >> N;
    a.resize(N);
    cin >> a;
}

void solve() {
    int64_t s = 0;
    for(int i = 0; i < N; i++) {
        s += a[i].first * 1ll * a[i].second * B;
        s %= L;
    }

    s = min(s, L - s);
    cout << setprecision(4) << fixed << (s / (double)B) << 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();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
statement.txt
======================
146. The Runner
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output



The runner moves along the ring road with length L. His way consists of N intervals. First he ran T1 minutes with speed V1, then T2 minutes with speed V2 and so on till the N-th interval, where he ran TN minutes with speed VN. Your task is to find the distance from start to finish along the ring road. The distance along the ring road is the length of the shortest way all points of which belongs to the ring road.

Input
Real number L (1<=L<=1000, with 4 signs after decimal point) and natural number N (N<=20000) are written in the first line. Each of the following N lines contains two integer numbers Ti and Vi (1<=Ti<=10^7, 1<=Vi<=10^6).

Output
Write the only one real number with 4 digits after decimal points: the distance from start to finish.

Sample test(s)

Input
2 1
1 3

Output
1.0000
Author:	Michael R. Mirzayanov
Resource:	Summer School Team Contest, 2002
Date:	August, 2002







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