#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <random>
#include <algorithm>
#include <ctype.h>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <functional>
using namespace std;
#define endl '\n' 
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> ii;
typedef vector<ii> vll;
const ll lim = 1e9 + 7;
const int N = 3e5 + 5;
const ll INF = 1e18;	
ll n, k;
ll a[N];
pair<long double, int> dp[N + 1][2];
bool check(long double lambda){
	dp[0][0] = {0, 0};
	dp[0][1] = {a[0] - lambda, 0};
	for (ll i = 1; 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].first + a[i], dp[i - 1][1].second),
						make_pair(dp[i - 1][0].first + a[i] - lambda, dp[i - 1][0].second + 1));
	}
	if(max(dp[n - 1][1], dp[n - 1][0]).second >= k)
		return 1;
	else
		return 0;
}
int main(){
	cin >> n >> k;
	for (ll i = 0; i < n; i++)
		cin >> a[i];
	
	long double l = 0, r = INF;
	while (r > l){
		long double mid = (l + r) / 2;
		if (check(mid))
			l = mid;
		else
			r = mid - 1;
	}
	check(l);
	cout << round(l * k + max(dp[n][1], dp[n][0]).first);
}
| # | 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... |