#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));
dp[0][0] = 0;
for (ll j = 1; j <= k; ++j) {
vll pref(n + 1, 0);
ll mx = -inf;
for (ll i = 1; i <= n; ++i) {
ll cost = b[i - 1], prof = s[i - 1];
pref[i] = pref[i - 1] + cost;
mx = max(mx, dp[i - 1][j - 1] + pref[i - 1]);
dp[i][j] = s[i - 1] - pref[i] + mx;
}
}
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... |