using namespace std;
#include <iostream>
#include <queue>
int N;
priority_queue<pair<long long,int>> pq;
int ans;
long long total = 0;
long long arr[1005];
int main() {
cin >> N;
ans = N;
for(int i = 0;i < N;i++) {
long long a,b;
cin >> a >> b;
pq.push({b,i});
total += a;
}
while(total > 0) {
ans--;
pair<long long,int> cur = pq.top();
pq.pop();
if(total > cur.first) {
arr[cur.second] = cur.first;
}else{
arr[cur.second] = total;
}
total -= cur.first;
}
cout << ans << endl;
for(int i = 0;i < N;i++) {
cout << arr[i] << " ";
}
cout << endl;
}