n = int(input())
T = []
Z = []
T1 = []
tSuma = 0
zSuma = 0
t = 0
c = 0
for i in range(n):
L = str(input())
L1, L2 = L.split(" ")
T.append(int(L1))
Z.append(int(L2))
T1.append(0)
tSuma += int(L1)
zSuma += int(L2)
Z1 = Z
Z1.sort(reverse = True)
for u in range(n):
if tSuma >= Z1[u]:
T1[Z.index(Z1[u])] = Z1[u]
tSuma -= Z1[u]
else:
T1[Z.index(Z1[u])] = tSuma
tSuma = 0
for k in range(len(T1)):
if T1[k] == 0:
c += 1
print(c)
print(T1)