#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; /** * @brief Convex Hull Trick Add Monotone * @docs docs/convex-hull-trick-add-monotone.md */ template< typename T, bool isMin > struct ConvexHullTrickAddMonotone { #define F first #define S second using P = pair< T, T >; deque< P > H; ConvexHullTrickAddMonotone() = default; bool empty() const { return H.empty(); } void clear() { H.clear(); } inline int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); } inline bool check(const P &a, const P &b, const P &c) { if(b.S == a.S || c.S == b.S) return sgn(b.F - a.F) * sgn(c.S - b.S) >= sgn(c.F - b.F) * sgn(b.S - a.S); //return (b.F-a.F)*(c.S-b.S) >= (b.S-a.S)*(c.F-b.F); if(is_integral< T >::value) { return (b.S - a.S) / (a.F - b.F) >= (c.S - b.S) / (b.F - c.F); } else { return (b.F - a.F) * sgn(c.S - b.S) / abs(b.S - a.S) >= (c.F - b.F) * sgn(b.S - a.S) / abs(c.S - b.S); } } void add(T a, T b) { if(!isMin) a *= -1, b *= -1; P line(a, b); if(empty()) { H.emplace_front(line); return; } if(H.front().F <= a) { if(H.front().F == a) { if(H.front().S <= b) return; H.pop_front(); } while(H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front(); H.emplace_front(line); } else { assert(a <= H.back().F); if(H.back().F == a) { if(H.back().S <= b) return; H.pop_back(); } while(H.size() >= 2 && check(H[H.size() - 2], H.back(), line)) H.pop_back(); H.emplace_back(line); } } inline T get_y(const P &a, const T &x) { return a.F * x + a.S; } T query(T x) { assert(!empty()); int l = -1, r = H.size() - 1; while(l + 1 < r) { int m = (l + r) >> 1; if(get_y(H[m], x) >= get_y(H[m + 1], x)) l = m; else r = m; } if(isMin) return get_y(H[r], x); return -get_y(H[r], x); } T query_monotone_inc(T x) { assert(!empty()); while(H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x)) H.pop_front(); if(isMin) return get_y(H.front(), x); return -get_y(H.front(), x); } T query_monotone_dec(T x) { assert(!empty()); while(H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x)) H.pop_back(); if(isMin) return get_y(H.back(), x); return -get_y(H.back(), x); } #undef F #undef S }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector

dapos, daneg; rep(i, n) { int a, d; cin >> a >> d; (d >= 0 ? dapos : daneg).emplace_back(d, a); } sort(all(dapos)); ConvexHullTrickAddMonotone cht; for (auto [d, a] : dapos) { cht.add(d, 2 * a - d); } int sz = daneg.size(); VL nxt(sz); for (int i = 0; auto [d, a] : daneg) { nxt[i] = a; i++; } auto cmp = [&](int i, int j) { return nxt[i] < nxt[j]; }; priority_queue q(cmp); rep(i, sz) q.emplace(i); ll ans = -8e18; if (!cht.empty()) chmax(ans, cht.query_monotone_dec(m) * m / 2); if (sz) { ll now = 0; for (int i = 1; i <= m; i++) { int j = q.top(); q.pop(); now += nxt[j]; nxt[j] += daneg[j].first; q.emplace(j); if (i == m) { chmax(ans, now); } else { if (!cht.empty()) chmax(ans, now + cht.query_monotone_dec(m - i) * (m - i) / 2); } } } cout << ans << '\n'; }