#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define pll pair<ll, ll>
#define tp tuple<ll, ll, ll>
const ll inf = 1e18;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll n, k, ans = -1e18;
cin >> n >> k;
vll b(n), s(n);
for (ll i = 0; i < n; ++i)
cin >> b[i];
for (ll i = 0; i < n; ++i)
cin >> s[i];
vector<vll> dp(n + 1, vll(k + 1, -inf));
for (ll i = 0; i <= n; ++i)
dp[i][0] = 0;
for (ll j = 1; j <= k; ++j) {
vll pref(n + 1, 0);
multiset<ll> st;
if (j == 1)
st.insert(0);
for (ll i = 1; i <= n; ++i) {
ll cost = b[i - 1], prof = s[i - 1];
pref[i] = pref[i - 1] + cost;
if (!st.empty())
dp[i][j] = prof - pref[i] - *st.begin();
if (dp[i][j - 1] > -inf / 2)
st.insert(-(dp[i][j - 1] + pref[i]));
}
}
for (ll i = 1; i <= n; ++i)
ans = max(ans, dp[i][k]);
cout << ans << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |