#include<bits/stdc++.h>
#define fi first
#define se second
#define ull unsigned long long
#define ll long long
#define BIT(a , b) (((a) >> (b)) & 1)
#define flip(a , b) ((a) ^ (1ll << (b)))
#define pii pair<int, int>
#define pb push_back
#define nl cout << "\n";
#define __ <<" "<<
#define mem(a, b) memset((a), (b), sizeof((a)))
#define all(c) (c).begin(), (c).end()
#define add(x , y) ((x) + (y) >= mod ? ((x) + (y) - mod) : ((x) + (y)))
#define sub(x , y) ((x) - (y) < 0 ? ((x) - (y) + mod) : ((x) - (y)))
#define file "F:/cp/test"
#define Times cerr << "\nTime run: " << clock() / 1000.0 << " ms\n";
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {if (a > b) {a = b; return 1;} return 0;}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {if (a < b) {a = b; return 1;} return 0;}
using namespace std;
const ll loo = 1e18 + 7;
const int oo = 1e9 + 7;
const int mod = 1e9 + 7;
const int N = 3e5 + 5;
const int LOG = 20;
const int base = 31;
void run_with_file(){
if(fopen(file".inp" , "r")){
freopen(file".inp", "r" , stdin);
freopen(file".out", "w" , stdout);
}
}
int n, a[N], k;
pair<ll, ll> dp[N][2];
void inp(){
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
}
void solve(){
ll l = 1;
ll r = loo;
ll res = 0;
while(l < r){
// cout << l << ' ' << r << '\n';
ll mid = (l + r) >> 1;
for(int i = 1; i <= n; i++) dp[i][0] = dp[i][1] = {0, 0};
dp[1][0] = {0, 0};
dp[1][1] = {a[1] - mid, 1};
for(int i = 2; i <= n; i++){
dp[i][0] = max(dp[i - 1][1], dp[i - 1][0]);
dp[i][1] = max(make_pair(dp[i - 1][1].fi + a[i], dp[i - 1][1].se),
make_pair(dp[i - 1][0].fi + a[i] - mid, dp[i - 1][0].se + 1));
}
pair<ll, ll> tmp = max(dp[n][1], dp[n][0]);
// for(int i = 1; i <= n; i++){
// cout << dp[i][1].fi << ' ' << dp[i][1]
// }
if(tmp.se >= k){
l = mid + 1;
res = max(res, tmp.fi + tmp.se * mid);
}
else r = mid - 1;
}
cout << res << '\n';
}
int main(){
cin.tie(0)->sync_with_stdio(false);
// run_with_file();
int test_case = 1;
//cin >> test_case;
while(test_case--){
inp();
solve();
}
}
/* UwU */
Compilation message (stderr)
feast.cpp: In function 'void run_with_file()':
feast.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen(file".inp", "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen(file".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... |