This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
const bool typetest = 0;
const int N = 2e3 + 5;
const ll Inf = 1e17;
int n, m;
struct Pakage
{
ll c, v;
bool operator<(const Pakage &a) const
{
return c < a.c;
}
} a[N];
ll dp[N][N];
void Read()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> a[i].v >> a[i].c;
sort(a + 1, a + n + 1);
}
template <class T>
void Max(T &x, T y)
{
if (x < y)
x = y;
}
void Solve()
{
fill_n(&dp[0][0], N * N, -Inf);
for (int i = 0; i <= n; ++i)
dp[i][0] = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= i && j <= m; ++j)
{
Max(dp[i][j], dp[i - 1][j]);
if (j == 1)
Max(dp[i][j], dp[i - 1][j - 1] + a[i].v + 2 * a[i].c);
else if (j == m)
Max(dp[i][j], dp[i - 1][j - 1] + a[i].v - 2 * a[i].c);
else
Max(dp[i][j], dp[i - 1][j - 1] + a[i].v);
}
cout << dp[n][m];
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
Read();
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... |