#include<bits/stdc++.h>
#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define dbg(x) "[" #x " = " << (x) << "]"
#define vi vector<int>
using namespace std;
bool START_ALLOC;
template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}
typedef long long ll;
typedef long double ld;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}
const int MAX = 2e5 + 15;
int n, k;
int a[MAX];
namespace AlienTrick{
#define pll pair<ll,ll>
pll f(ll penalty){
vector<vector<pll>> dp(n, vector<pll>(2, {0, 0}));
dp[0][0] = {0, 0};
dp[0][1] = {a[0] - penalty, 1};
for(int i = 1; i < n; i++){
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = max(make_pair(dp[i-1][0].fi + a[i] - penalty, dp[i-1][0].se + 1),
make_pair(dp[i-1][1].fi + a[i], dp[i-1][1].se));
}
return max(dp[n-1][0], dp[n-1][1]);
}
ll query(){
ll lo = 1, hi = 3e15, res = 0;
int counter = 0;
while(lo <= hi){
ll mid = (lo + hi) >> 1;
if(f(mid).se >= k){
res = mid;
lo = mid+1;
}
else hi = mid-1;
}
return f(res).fi + 1LL*res*k;
}
}
void solve(){
cin >> n >> k;
for(int i = 0; i < n; i++) cin >> a[i];
cout << AlienTrick::query() << endl;
}
bool END_ALLOC;
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
cerr << "[Static Memory : " << fixed << setprecision(2) << (((&END_ALLOC) - (&START_ALLOC)) / (1024.0 * 1024.0)) << "mb]\n";
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t; t = 1; //cin >> t;
while(t--) solve();
}
컴파일 시 표준 에러 (stderr) 메시지
feast.cpp: In function 'int main()':
feast.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | 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... |