Minimum Average Waiting Time
Difficulty: Hard

Tieu owns a pizza restaurant and he manages it in his own way.  While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. 

Different kinds of pizzas take different amounts of time to cook. Also, once he starts cooking a pizza, he cannot cook another pizza until the first pizza is completely cooked. Let's say we have three customers who come at time t=0, t=1, & t=2 respectively, and the time needed to cook their pizzas is 3, 9, & 6 respectively. If Tieu applies first-come, first-served rule, then the waiting time of three customers is 3, 11, & 16  respectively. The average waiting time in this case is (3 + 11 + 16) / 3 = 10. This is not an optimized solution. After serving the first customer at time t=3, Tieu can choose to serve the third customer. In that case, the waiting time will be 3, 7, & 17 respectively. Hence the average waiting time is (3 + 7 + 17) / 3 = 9.

Help Tieu achieve the minimum average waiting time. For the sake of simplicity, just find the integer part of the minimum average waiting time.

**Input Format**

* The first line contains an integer N, which is the number of customers. 
* In the next N lines, the i<sup>th</sup> line contains two space separated numbers T<sub>i</sub> and L<sub>i</sub>. T<sub>i</sub> is the time when i<sup>th</sup> customer order a pizza, and L<sub>i</sub> is the time required to cook that pizza.  

- The $i^{th}$ customer is not the customer arriving at the $i^{th}$ arrival time. 

**Output Format**

* Display the integer part of the minimum average waiting time.

**Constraints**

* 1 ≤ N ≤ 10<sup>5</sup>
* 0 ≤ T<sub>i</sub> ≤ 10<sup>9</sup>
* 1 ≤ L<sub>i</sub> ≤ 10<sup>9</sup>

**Note**

* The waiting time is calculated as the difference between the time a customer orders pizza (the time at which they enter the shop) and the time she is served.

* Cook does not know about the future orders.

**Sample Input #00**

    3
    0 3
    1 9
    2 6

**Sample Output #00**

	9

**Sample Input #01**

	3
	0 3
	1 9
	2 5

**Sample Output #01**
	
    8

**Explanation #01**

Let's call the person ordering at time = 0 as *A*, time = 1 as *B* and time = 2 as *C*. By delivering pizza for *A*, *C* and *B* we get the minimum average wait time to be 

    (3 + 6 + 16)/3 = 25/3 = 8.33 
    
the integer part is `8` and hence the answer. 

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:
#
# Complete the 'minimumAverage' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY customers as parameter.
#

def minimumAverage(customers):
    # Write your code here




Reference solution (C++):
    /*
    Solution: pick the order that takes shortest time to cook.
    */
    #include <cstdio>
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <set>
    #include <map>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <memory.h>
    #include <sstream>
    #include <complex>
    #include <cassert>
    #include <climits>

    #define REP(i,n) for(int i = 0, _n = (n); i < _n; i++)
    #define REPD(i,n) for(int i = (n) - 1; i >= 0; i--)
    #define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
    #define FORD(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
    #define FORN(i,a,b) for(int i=a;i<b;i++)
    #define FOREACH(it,c) for (__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
    #define RESET(c,x) memset (c, x, sizeof (c))

    #define PI acos(-1)
    #define sqr(x) ((x) * (x))
    #define PB push_back
    #define MP make_pair
    #define F first
    #define S second
    #define Aint(c) (c).begin(), (c).end()
    #define SIZE(c) (c).size()

    #define DEBUG(x) { cerr << #x << " = " << x << endl; }
    #define PR(a,n) {cerr<<#a<<" = "; FOR(_,1,n) cerr << a[_] << ' '; cerr <<endl;}
    #define PR0(a,n) {cerr<<#a<<" = ";REP(_,n) cerr << a[_] << ' '; cerr << endl;}
    #define LL long long

    using namespace std;

    #define maxn 100111
    int n;
    pair<int, int> order[maxn];
    multiset<pair<int, int> > available_order;

    bool getChar(char& c) {
        return (scanf("%c", &c) == 1);
    }

    void nextInt(long long& u, char endline, int l, int r) {
        int sign = 1;
        long long sum = 0;
        char c;
        assert(getChar(c));
        if (c == '-') sign = -1;
        else {
            assert('0' <= c && c <= '9');
            sum = (c - '0');
        }

        int cnt = 0;

        for (int i = 1; i <= 15; i++) {
            assert(getChar(c));
            if (c == endline) break;
            assert('0' <= c && c <= '9');
            sum = sum * 10 + (c - '0');
            cnt ++;
            assert(cnt <= 15);
        }

        sum = sign * sum;
        assert (l <= sum && sum <= r);
        u = sum;
    }

    void nextInt(int& u, char endline, int l, int r) {
        long long tmp;
        nextInt(tmp, endline, l, r);
        u = tmp;
    }

    int main() {
        //freopen("a.in", "rb", stdin); freopen("a.out", "wb", stdout);
        nextInt(n, '\n', 1, 1e5);
        for (int i = 0; i < n; i++) {
            nextInt(order[i].first, ' ', 0, 1e9);
            nextInt(order[i].second, '\n', 1, 1e9);
        }

        //sort the orders in the increaseing order of the submit time.
        sort(order, order + n);

        //we maintain the available_order set, which stores the submitted order in the increasing order of the time to cook
        int pos = 0;//pos is the next order to be considered to be push in available_order
        long long current_time = order[0].first;
        //the total waiting time may be out of 64bit integer range so we maintain two values average and mod so that total waiting time = average * n + mod.
        long long average = 0, mod = 0;

        for (int i = 0; i < n; i++) {
            //push all submitted orders into available_order
            while (pos < n && order[pos].first <= current_time) {
                //note that in available_order, the order is stored in the increasing order of time to cook
                available_order.insert(make_pair(order[pos].second, order[pos].first));
                pos++;
            }

            //take out the order that has a shortest time to cook
            pair<int, int> item = *available_order.begin();

            available_order.erase(available_order.begin());

            //update current_time, average and mod
            current_time += item.first;
            //this is the waiting_time of the current order
            long long waiting_time = current_time - item.second;

            mod += waiting_time % n;
            average += waiting_time / n;

            if (mod >= n) {
                average += mod / n;
                mod %= n;
            }
        }

        cout << average << endl;
        char __c;
        assert(!getChar(__c));
        return 0;
    }