#include #include using namespace std; using namespace atcoder; #define rep(i, l, r) for (ll i = (l); i < (r); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() using ll = long long; using ull = unsigned long long; using ld = long double; using pl = pair; using vi = vector; using vl = vector; using vvl = vector>; using vvvl = vector>>; template using pq_ = priority_queue, greater>; #define sz(x) (int)(x).size() typedef pair pii; using mint=modint998244353; // g++ a.cpp -std=c++23 -I. // g++ -std=c++23 -I. a.cpp -o main // g++ -std=c++23 -I. anaive.cpp -o naive // g++ -std=c++23 -I. agene.cpp -o gene int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); ll n,d; cin>>n>>d; vl a(n); vl b(n); rep(i,0,n){ cin>>a[i]>>b[i]; } vector> e(n); rep(i,0,n){ e[i]={b[i],a[i]}; } sort(all(e)); reverse(all(e)); ll r=0; ll now=0; ll ans=0; rep(i,0,n){ if(now+e[i][1]>=d){ break; } else { r++; now+=e[i][1]; } } if(r!=n)ans+=e[r][0]; vector> g; rep(i,0,r){ e[i][0]-=ans; } rep(i,0,r){ ans+=e[i][0]/d*e[i][1]; e[i][0]%=d; g.push_back({e[i][0],e[i][1]}); } sort(all(g)); if(r!=0)d=g[r-1][0]; ll anss=ans; vl rui(r+1); rep(i,0,r){ rui[i+1]+=rui[i]+g[i][1]; } ans=ans+rui[r]; rep(i,0,r){ ans=min(ans,anss+g[i][0]+rui[r]-rui[i+1]); } cout<