#include using namespace std; #define all(v) (v).begin(),(v).end() #define pb emplace_back #define rep(i, n) for(int i=0;i<(n);i++) #define foa(e, v) for(auto& e : v) #define dout(a) cout< using pqr = priority_queue, greater>; template inline bool chmax(T1 &a, T2 b) { bool compare = a < b; if(compare) a = b; return compare; } template inline bool chmin(T1 &a, T2 b) { bool compare = a > b; if(compare) a = b; return compare; } template inline T back(std::set &s) { return *s.rbegin(); } template inline T back(std::multiset &s) { return *s.rbegin(); } template inline T pop_back(std::set &s) { auto it = prev(s.end()); T val = *it; s.erase(it); return val; } template inline T pop_back(std::multiset &s) { auto it = prev(s.end()); T val = *it; s.erase(it); return val; } const int dy[8] = {-1, 0, 0, 1, 1, -1, 1, -1}; const int dx[8] = {0, -1, 1, 0, -1, -1, 1, 1}; const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (3LL << 59); const int inf = 1 << 30; const char br = '\n'; ll calc1(vector a, vector b, ll d) { ll n = a.size(); ll ans = 0; rep(i, n) { ans += (a[i] - 1) / d * b[i]; a[i] -= ((a[i] - 1) / d) * d; } vector ord(n, 0); iota(all(ord), 0); sort(all(ord), [&](int i, int j) { return a[i] < a[j]; }); ll sum = ans; // cout << sum << " " << a[ord.back()] << endl; ans = a[ord.back()] + sum; for(int i = n - 1; i >= 0; i --) { sum += b[i]; chmin(ans, sum + (i == 0 ? 0LL : a[i - 1])); } return ans; } void solve() { ll n, d; cin >> n >> d; map mp; ll ans = 0; rep(i, n) { ll a, b; cin >> a >> b; mp[b] += a; chmax(ans, b); } vector a, b; for(auto [x, y] : mp) { a.pb(x); b.pb(y); } chmin(ans, calc1(a, b, d)); ll sum = 0; ll base = -1; n = a.size(); for(int i = n - 1; i >= 0; i --) { sum += b[i]; if(sum >= d) { base = a[i]; break; } } priority_queue> pq; rep(i, n) pq.push({a[i], b[i]}); sum = 0; while(base >= 0LL) { auto [x, y] = pq.top(); pq.pop(); // cout << x << " " << y << " " << base << " " << sum << endl; if(x <= 0) { chmin(ans, sum); break; } chmin(ans, x + sum); if(x > base + d) { ll num = max(1LL, (x - (base + d)) / d); sum += num * y; x -= num * d; pq.push({x, y}); } else if(x < base - d) break; else { sum += y; x -= d; pq.push({x, y}); } } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); int testcase = 1; // cin >> testcase; while(testcase --) solve(); return 0; }