#include <iostream>
#include <utility>
#include <algorithm>

using namespace std;

bool comp (pair <int, int> a, pair <int, int> b)
{
	return (a.second < b.second);
}

int main ()
{
	cin.tie (NULL);
	ios::sync_with_stdio (false);
	int n;
	cin >> n;
	pair <int, int>* monitors = new pair <int, int> [n];
	for (int i = 0 ; i < n ; i ++)
	{
		cin >> monitors [i].first >> monitors [i].second;
	}
	sort (monitors + 0, monitors + n, comp);
	int max = 0;
	int current = 0;
	for (int i = 0 ; i < n ; i ++)
	{
		if (i != 0 and monitors [i].second != monitors [i - 1].second)
		{
			current = 0;
		}
		current += monitors [i].first;
		if (max < current) max = current;
		cout << monitors [i].first << " " << monitors [i].second << "\n";
	}
	cout << max << " ";
}
