#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define sui cout.tie(NULL); cin.tie(NULL); ios_base::sync_with_stdio(false)
const int MAX_N = 1e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const ld EPS = 1e-9;
const int LOG = 30;
void solve() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> vs;
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
vs.push_back(make_pair(b, a));
}
sort(all(vs));
ll ans = -INF;
multiset<int> inds[n];
ll sum[n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i - m + 1; j++) ans = max(ans, (ll) vs[i].second + sum[j] - 2 * (vs[i].first - vs[j].first));
sum[i] = vs[i].second;
for (int j = 0; j < i; j++) if (inds[j].size() < m - 2 || *inds[j].begin() < vs[i].second)
{
if (inds[j].size() >= m - 2)
{
sum[j] -= *inds[j].begin();
inds[j].erase(inds[j].begin());
}
sum[j] += vs[i].second;
inds[j].insert(vs[i].second);
}
}
cout << ans << "\n";
}
int main() {
sui;
int tc = 1;
//cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |