#P9121. Bessie's Haybale Feast
Bessie's Haybale Feast
Bessie's Haybale Feast
Bessie is a hungry cow. Each day at dinner, if there is at least one haybale in the barn, she eats exactly one haybale. Farmer John ensures Bessie never starves by sending haybale deliveries on certain days. On day \(d_i\), Farmer John sends \(b_i\) haybales in the morning (before dinner). Given the schedule of these deliveries, compute the total number of haybales Bessie will consume during the first \(T\) days.
Note: On any day when a delivery occurs, the haybales are added to the barn in the morning, and then Bessie may have dinner (and eat one haybale, if available) later that day. If no haybale is available at dinner time, she does not eat on that day.
inputFormat
The first line contains two space-separated integers \(n\) and \(T\) \((1 \leq n \leq 10^5,\ 1 \leq T \leq 10^{14})\), representing the number of haybale deliveries and the total number of days, respectively.
Each of the next \(n\) lines contains two space-separated integers \(d_i\) and \(b_i\) \((1 \leq d_i \leq 10^{14},\ 1 \leq b_i \leq 10^9)\), where \(d_i\) is the day a delivery is made and \(b_i\) is the number of haybales delivered on that day. The deliveries are not necessarily given in sorted order.
outputFormat
Output a single integer, the total number of haybales Bessie will eat during the first \(T\) days.
sample
1 5
3 5
3