//***** author: attacker *****//
//***** created: 11.04.2026 15:21:57 *****//
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 1
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpc __builtin_popcount
#define size(v) (int) (v.size())
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
vector<pair<int64_t, int>> a(n);
int64_t sum = 0;
for (int i = 0; i < n; i++) {
int t, z;
cin >> t >> z;
sum += t;
a[i] = {z, i};
}
sort(a.rbegin(), a.rend());
vector<int64_t> res(n);
for (auto [t, i] : a) {
if (sum >= t) {
res[i] = t;
sum -= t;
} else {
res[i] = sum;
sum = 0;
}
}
cout << count(res.begin(), res.end(), 0) << '\n';
for (int i = 0; i < n; i++) {
cout << res[i] << " \n"[i == n - 1];
}
return 0;
}