Problem:
Score : 600 points

Problem Statement

There is a function f(x), which is initially a constant function f(x) = 0.

We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:

An update query 1 a b: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x).

An evaluation query 2: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value.

We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.

Constraints

All values in input are integers.

1 \leq Q \leq 2 \times 10^5

-10^9 \leq a, b \leq 10^9

The first query is an update query.

Input

Input is given from Standard Input in the following format:

Q
Query_1
:
Query_Q

See Sample Input 1 for an example.

Output

For each evaluation query, print a line containing the response, in the order in which the queries are given.

The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between.

Sample Input 1

4
1 4 2
2
1 1 -8
2

Sample Output 1

4 2
1 -3

In the first evaluation query, f(x) = |x - 4| + 2, which attains the minimum value of 2 at x = 4.

In the second evaluation query, f(x) = |x - 1| + |x - 4| - 6, which attains the minimum value of -3 when 1 \leq x \leq 4. Among the multiple values of x that minimize f(x), we ask you to print the minimum, that is, 1.

Sample Input 2

4
1 -1000000000 1000000000
1 -1000000000 1000000000
1 -1000000000 1000000000
2

Sample Output 2

-1000000000 3000000000

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

template <class VAL>
struct RBST
{
    VAL SUM_UNITY = 0; // to be set

    unsigned int randInt()
    {
        static unsigned int tx = 123456789, ty = 362436069, tz = 521288629, tw = 88675123;
        unsigned int tt = (tx ^ (tx << 11));
        tx = ty;
        ty = tz;
        tz = tw;
        return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
    }

    struct NODE
    {
        NODE *left, *right;
        VAL val;  // the value of the node
        int size; // the size of the subtree
        VAL sum;  // the value-sum of the subtree

        NODE() : val(SUM_UNITY), size(1), sum(SUM_UNITY)
        {
            left = right = NULL;
        }

        NODE(VAL v) : val(v), size(1), sum(v)
        {
            left = right = NULL;
        }

        /* additional update */
        inline void update()
        {
        }

        /* additional lazy-propagation */
        inline void push()
        {

            /* ex: reverse */
            /*
            if (this->rev) {
            swap(this->left, this->right);
            if (this->left) this->left->rev ^= true;
            if (this->right) this->right->rev ^= true;
            this->rev = false;
            }
            */
        }
    };

    ///////////////////////
    // root
    ///////////////////////

    NODE *root;
    RBST() : root(NULL) {}
    RBST(NODE *node) : root(node) {}

    ///////////////////////
    // basic operations
    ///////////////////////

    /* size */
    inline int size(NODE *node)
    {
        return !node ? 0 : node->size;
    }
    inline int size()
    {
        return this->size(this->root);
    }

    /* sum */
    inline VAL sum(NODE *node)
    {
        return !node ? SUM_UNITY : node->sum;
    }
    inline VAL sum()
    {
        return this->sum(this->root);
    }

    /* update, push */
    inline NODE *update(NODE *node)
    {
        node->size = size(node->left) + size(node->right) + 1;
        node->sum = sum(node->left) + sum(node->right) + node->val;
        node->update();
        return node;
    }

    inline void push(NODE *node)
    {
        if (!node)
            return;
        node->push();
    }

    /* lower_bound */
    inline int lowerBound(NODE *node, VAL val)
    {
        push(node);
        if (!node)
            return 0;
        if (val <= node->val)
            return lowerBound(node->left, val);
        else
            return size(node->left) + lowerBound(node->right, val) + 1;
    }
    inline int lowerBound(VAL val)
    {
        return this->lowerBound(this->root, val);
    }

    /* upper_bound */
    inline int upperBound(NODE *node, VAL val)
    {
        push(node);
        if (!node)
            return 0;
        if (val >= node->val)
            return size(node->left) + upperBound(node->right, val) + 1;
        else
            return upperBound(node->left, val);
    }
    inline int upperBound(VAL val)
    {
        return this->upperBound(this->root, val);
    }

    /* count */
    inline int count(VAL val)
    {
        return upperBound(val) - lowerBound(val);
    }

    /* get --- k: 0-index */
    inline VAL get(NODE *node, int k)
    {
        push(node);
        if (!node)
            return -1;
        if (k == size(node->left))
            return node->val;
        if (k < size(node->left))
            return get(node->left, k);
        else
            return get(node->right, k - size(node->left) - 1);
    }
    inline VAL get(int k)
    {
        return get(this->root, k);
    }

    ///////////////////////
    // merge-split
    ///////////////////////

    NODE *merge(NODE *left, NODE *right)
    {
        push(left);
        push(right);
        if (!left || !right)
        {
            if (left)
                return left;
            else
                return right;
        }
        if (randInt() % (left->size + right->size) < left->size)
        {
            left->right = merge(left->right, right);
            return update(left);
        }
        else
        {
            right->left = merge(left, right->left);
            return update(right);
        }
    }
    void merge(RBST add)
    {
        this->root = this->merge(this->root, add.root);
    }
    pair<NODE *, NODE *> split(NODE *node, int k)
    { // [0, k), [k, n)
        push(node);
        if (!node)
            return make_pair(node, node);
        if (k <= size(node->left))
        {
            pair<NODE *, NODE *> sub = split(node->left, k);
            node->left = sub.second;
            return make_pair(sub.first, update(node));
        }
        else
        {
            pair<NODE *, NODE *> sub = split(node->right, k - size(node->left) - 1);
            node->right = sub.first;
            return make_pair(update(node), sub.second);
        }
    }
    RBST split(int k)
    {
        pair<NODE *, NODE *> sub = split(this->root, k);
        this->root = sub.first;
        return RBST(sub.second);
    }

    ///////////////////////
    // insert-erase
    ///////////////////////

    void insert(const VAL val)
    {
        pair<NODE *, NODE *> sub = this->split(this->root, this->lowerBound(val));
        this->root = this->merge(this->merge(sub.first, new NODE(val)), sub.second);
    }

    void erase(const VAL val)
    {
        if (!this->count(val))
            return;
        pair<NODE *, NODE *> sub = this->split(this->root, this->lowerBound(val));
        this->root = this->merge(sub.first, this->split(sub.second, 1).second);
    }

    ///////////////////////
    // debug
    ///////////////////////

    void print(NODE *node)
    {
        if (!node)
            return;
        push(node);
        print(node->left);
        cout << node->val << " ";
        print(node->right);
    }
    void print()
    {
        cout << "{";
        print(this->root);
        cout << "}" << endl;
    }
};

int main()
{
    ll Q;
    cin >> Q;
    ll B = 0;
    ll xi = 0;
    ll s = 0;
    RBST<ll> T;
    REP(i, Q)
    {
        int q;
        cin >> q;
        if (q == 1)
        {
            ll a, b;
            cin >> a >> b;
            B += b;
            ll size = T.size();
            if (size > 0 && T.get(xi) > a)
                xi++;
            T.insert(a);
            size++;
            s += abs(T.get(xi) - a);
            ll nxi = (size - 1) / 2;
            ll p = T.get(nxi) - T.get(xi);
            if (T.get(nxi) == a)
            {

                    s -= abs(T.get(xi) - a);
            }
 //           else
            {
                s += nxi * p - (size - 1 - nxi) * p;
            }

            xi = nxi;
        }
        else
        {
            cout << T.get(xi) << " " << s + B << endl;
        }
    }
    return 0;
}