#include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); ll N, M; cin >> N >> M; auto sum = [&](ll a, ll d) { return (a + a + (M - 1) * d) * M / 2; }; vector A(N), D(N); for(ll i = 0; i < N; i++) { cin >> A[i] >> D[i]; } ll m = 0, idx = -1; for(ll i = 0; i < N; i++) { if(D[i] < 0) { continue; } ll tmp = sum(A[i], D[i]); if(tmp > m) { idx = i; m = tmp; } else if(tmp == m) { if(idx == -1 || D[idx] > D[i]) { idx = i; } } } if(idx == -1) { priority_queue> q; for(ll i = 0; i < N; i++) { q.emplace(A[i], D[i]); } ll ans = 0; for(ll i = 0; i < M; i++) { auto [t, d] = q.top(); q.pop(); ans += t; q.emplace(t + d, d); } cout << ans << "\n"; } else { vector single; for(ll i = 0; i < M; i++) { single.emplace_back(A[idx] + D[idx] * i); } priority_queue> q; for(ll i = 0; i < N; i++) { if(D[i] < 0) { q.emplace(A[i], D[i]); } } ll ans = m; for(ll i = 0; i < M; i++) { auto [t, d] = q.top(); q.pop(); if(t > single.back()) { ans += t - single.back(); single.pop_back(); q.emplace(t + d, d); } else { break; } } cout << ans << "\n"; } }