#include <bits/stdc++.h>
#define ar array
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const int maxn = 1e5 + 5;
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, m; cin >> n >> m;
vector<ar<int, 2> > a(n+1);
for(int i=1; i<=n; i++) cin >> a[i][1] >> a[i][0];
sort(a.begin()+1, a.end());
ll dp[n+1][m+1];
for(int i=0; i<=n; i++)
for(int j=0; j<=m; j++) dp[i][j] = -1e18;
dp[0][0] = 0;
for(int i=1; i<=n; i++) {
for(int j=0; j<=m; j++) {
dp[i][j] = dp[i-1][j];
ll cost = a[i][1];
if(j == 1) cost += 2ll * a[i][0];
if(j == m) cost -= 2ll * a[i][0];
if(j) dp[i][j] = max(dp[i][j], dp[i-1][j-1] + cost);
}
}
cout << dp[n][m] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |