#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define BIT(mask, x) ((mask >> (x)) & 1)
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] = ", debug(__VA_ARGS__)
template<typename T> void debug(T x) { cerr << x << "\n"; }
template<typename T, typename... Args> void debug(T x, Args... args) {
cerr << x << ", "; debug(args...);
}
// -------------------------------------- CON CUA BO HCN --------------------------------------
const int maxn = 3e5 + 3;
int n, a[maxn], K;
pll dp[maxn][2];
// --------------------------------------------------------------------------------------------
pll check(const int lmb)
{
FOR(i, 1, n)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(pll(dp[i - 1][0].fi + a[i] - lmb, dp[i - 1][0].se + 1), pll(dp[i - 1][1].fi + a[i], dp[i - 1][1].se));
}
return max(dp[n][0], dp[n][1]);
}
void solve()
{
cin >> n >> K;
FOR(i, 1, n) cin >> a[i];
dp[0][1] = {-1e15, -1e15};
ll l = 1, r = 1e15;
while (l <= r)
{
ll mid = l + r >> 1;
if (check(mid).second >= K) l = mid + 1;
else r = mid - 1;
}
cout << check(r).first + K * r;
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define TASK "TEST"
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
solve();
return 0;
}
/* NOTES:
*/
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen(TASK".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |