#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } /** * verified: https://atcoder.jp/contests/abc228/submissions/27464591 */ template class ConvexHullTrick{ public: T x; ConvexHullTrick(){ x = 0; } /** * xを一つ進めます */ void next(){ x++; while(dq.size() >= 2 && f(x, dq[0]) >= f(x, dq[1])) dq.pop_front(); } /** * xを指定された値まで進めます */ void proceed(T _x){ assert(x <= _x); x = _x; while(dq.size() >= 2 && f(x, dq[0]) >= f(x, dq[1])) dq.pop_front(); } /** * 直線 y = p_add*x+q を追加します(今まで追加した中で傾きが最小の直線であること) */ void add_line(T p_add, T q_add){ if(!dq.empty()) assert(p_add <= p[dq.back()]); p.push_back(p_add); q.push_back(q_add); int n_lines = p.size(); while(dq.size() >= 2 && check(dq[dq.size()-2], dq.back(), n_lines-1)) { dq.pop_back(); } dq.push_back(n_lines-1); } T get_min(){ return f(x, dq[0]); } private: vector p, q; // 直線p[i]x+q[i] deque dq; T f(int x, int i){ return p[i]*x+q[i]; } bool check(int i, int j, int k){ return (p[j]-p[i])*(q[k]-q[j]) >= (q[j]-q[i])*(p[k]-p[j]); }; }; using P = pair; const ll inf = 4e18; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n; ll m; cin >> n >> m; vector a(n), d(n); vector pos, neg; for(int i = 0; i < n; i++){ cin >> a[i] >> d[i]; a[i] *= 2; d[i] *= 2; if(d[i] >= 0){ pos.push_back(i); }else{ neg.push_back(i); } } ll ans = -inf; if(neg.empty()){ for(int i = 0; i < n; i++){ ll sum = (d[i]/2)*m*m-(d[i]/2)*m+a[i]*m; chmax(ans, sum); } cout << ans/2 << endl; return 0; } vector sum_neg(m+1); priority_queue

que; vector cnt(n); for(int i: neg){ que.push({a[i], i}); } for(int i = 0; i < m; i++){ auto [x, idx] = que.top(); que.pop(); cnt[idx]++; sum_neg[i+1] = sum_neg[i]+x; que.push({a[idx]+cnt[idx]*d[idx], idx}); } if(pos.empty()){ cout << sum_neg[m]/2 << endl; return 0; } vector

vp; for(int i: pos){ vp.push_back({-d[i]/2, d[i]/2-a[i]}); } sort(vp.begin(), vp.end(), greater

()); ConvexHullTrick cht; for(auto [p, q]: vp){ cht.add_line(p, q); } for(ll x = 0; x <= m; x++){ cht.proceed(x); chmax(ans, sum_neg[m-x]-cht.get_min()*x); } cout << ans/2 << endl; }