#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;
vll pref(n + 1, 0);
for (ll i = 1; i <= n; ++i) pref[i] = pref[i - 1] + b[i - 1];
for (ll j = 1; j <= k; ++j) {
ll best = (j == 1 ? 0 : inf);
for (ll i = 1; i <= n; ++i) {
if (best != inf) dp[i][j] = s[i - 1] - pref[i] - best;
if (dp[i][j - 1] > -inf / 2) best = min(best, -(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... |