#line 1 "test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/2546" #include using namespace std; #line 4 "https://drken1215.github.io/algorithm/template.hpp.html" using namespace std; // My library is in https://github.com/drken1215/algorithm #line 4 "https://drken1215.github.io/algorithm/Others/macro.hpp.html" using namespace std; using ll = long long; using i128 = __int128_t; using u128 = __uint128_t; using pint = pair; using pll = pair; using tll = array; using fll = array; using vint = vector; using vll = vector; using dint = deque; using dll = deque; using vvint = vector>; using vvll = vector>; using vpll = vector>; template using min_priority_queue = priority_queue, greater>; template inline bool chmax(S &a, T b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(S &a, T b) { return (a > b ? a = b, 1 : 0); } template inline auto maxll(S a, T b) { return max(ll(a), ll(b)); } template inline auto minll(S a, T b) { return min(ll(a), ll(b)); } template auto max(const T &a) { return *max_element(a.begin(), a.end()); } template auto min(const T &a) { return *min_element(a.begin(), a.end()); } template auto argmax(const T &a) { return max_element(a.begin(), a.end()) - a.begin(); } template auto argmin(const T &a) { return min_element(a.begin(), a.end()) - a.begin(); } template auto accum(const vector &a) { return accumulate(a.begin(), a.end(), T()); } template auto accum(const deque &a) { return accumulate(a.begin(), a.end(), T()); } #define REP(i, a) for (long long i = 0; i < (long long)(a); i++) #define REP2(i, a, b) for (long long i = a; i < (long long)(b); i++) #define RREP(i, a) for (long long i = (a)-1; i >= (long long)(0); --i) #define RREP2(i, a, b) for (long long i = (b)-1; i >= (long long)(a); --i) #define EB emplace_back #define PF push_front #define PB push_back #define MP make_pair #define FI first #define SE second #define ALL(x) x.begin(), x.end() #line 3 "https://drken1215.github.io/algorithm/Others/stl_container_stream.hpp.html" using namespace std; // input stream template istream& operator >> (istream &is, vector &P) { for (int i = 0; i < (int)P.size(); ++i) cin >> P[i]; return is; } template istream& operator >> (istream &is, deque &P) { for (int i = 0; i < (int)P.size(); ++i) cin >> P[i]; return is; } template istream& operator >> (istream &is, vector> &P) { for (int i = 0; i < (int)P.size(); ++i) cin >> P[i]; return is; } // output stream #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template ostream& operator << (ostream &s, const pair &P) { return s << '<' << P.first << ", " << P.second << '>'; } template ostream& operator << (ostream &s, const array &P) { return s << '<' << P[0] << "," << P[1] << '>'; } template ostream& operator << (ostream &s, const array &P) { return s << '<' << P[0] << "," << P[1] << "," << P[2] << '>'; } template ostream& operator << (ostream &s, const array &P) { return s << '<' << P[0] << "," << P[1] << "," << P[2] << "," << P[3] << '>'; } template ostream& operator << (ostream &s, const vector &P) { for (int i = 0; i < P.size(); ++i) { s << P[i] << endl; } return s; } template ostream& operator << (ostream &s, const vector &P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, const deque &P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, const vector> &P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template ostream& operator << (ostream &s, const set &P) { for (auto it : P) { s << "<" << it << "> "; } return s; } template ostream& operator << (ostream &s, const multiset &P) { for (auto it : P) { s << "<" << it << "> "; } return s; } template ostream& operator << (ostream &s, const unordered_set &P) { for (auto it : P) { s << "<" << it << "> "; } return s; } template ostream& operator << (ostream &s, const map &P) { for (auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s; } template ostream& operator << (ostream &s, const unordered_map &P) { for (auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s; } // Yes/No void yes(bool a) { cout << (a ? "yes" : "no") << endl; } void YES(bool a) { cout << (a ? "YES" : "NO") << endl; } void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; } #line 4 "https://drken1215.github.io/algorithm/Optimization/monotone_convex_hull_trick.hpp.html" using namespace std; // Convex Hull Trick /* 追加クエリの直線の傾きが単調減少の場合 - insert(a, b): add y = ax + b, O(1) - query(x): min_i{a[i]*x + b}, O(log N) - query_monotone(x): min_i{a[i]*x + b}, O(1), x も単調増加であることを仮定 */ template struct MonotoneConvexHullTrick { using Line = pair; deque lines; T INF; MonotoneConvexHullTrick(T inf = numeric_limits::max() / 2) : INF(inf) {} bool check(const Line &a, const Line &b, const Line &c) { return (b.first - a.first) * (c.second - b.second) >= (b.second - a.second) * (c.first - b.first); } T get(int k, T x) { if (lines.empty() || k >= lines.size()) return INF; assert(k >= 0 && k < lines.size()); return lines[k].first * x + lines[k].second; } void insert(T a, T b) { Line l(a, b); while (lines.size() >= 2 && check(lines[(int)lines.size()-2], lines[(int)lines.size()-1], l)) { lines.pop_back(); // 不要な直線を捨てる } lines.push_back(l); } T query(T x) { int low = -1, high = (int)lines.size(); while (high - low > 1) { int mid = (low + high) / 2; if (get(mid, x) >= get(mid + 1, x)) low = mid; else high = mid; } return get(high, x); } // クエリの単調性も成り立つ場合 (x が単調増加) T query_monotone(T x) { while (lines.size() >= 2 && get(0, x) >= get(1, x)) lines.pop_front(); if (lines.empty()) return INF; return lines[0].first * x + lines[0].second; } }; #line 8 "test.cpp" /* f[a,d](m) = (A*2 + D(m-1)) * m / 2 = (-Dm + d-2A) * (-m/2) */ int main() { ll N, M, a, d, INF = 1LL << 60; cin >> N >> M; vll PA, PD; priority_queue que; REP(i, N) { cin >> a >> d; if (d >= 0) PA.EB(a), PD.EB(d); else que.push(pll(a, d)); } vll ids(PA.size()); iota(ALL(ids), 0); sort(ALL(ids), [&](int i, int j) { return -PD[i] > -PD[j]; }); vll plus(M+1, -INF), minus(M+1, -INF); MonotoneConvexHullTrick cht; for (auto i : ids) cht.insert(-PD[i], PD[i] - PA[i] * 2); plus[0] = minus[0] = 0; for (int m = 1; m <= M; m++) { if (PA.size()) plus[m] = -cht.query_monotone(m) * m / 2; if (!que.empty()) { auto [s, d] = que.top(); que.pop(); minus[m] = minus[m-1] + s; que.push(pll(s+d, d)); } } ll res = -INF; REP(m, M+1) chmax(res, plus[m] + minus[M-m]); cout << res << endl; }