Submission #1269500

#TimeUsernameProblemLanguageResultExecution timeMemory
1269500miaowtinFeast (NOI19_feast)C++20
0 / 100
147 ms14452 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int maxn = 300005;

LL a[maxn], sum[maxn], dp[maxn][2], cnt[maxn][2];
int n, k;

pair<LL, LL> solve(LL penalty) {
	memset(dp, 0, sizeof(dp));
	memset(cnt, 0, sizeof(cnt));
	dp[1][1] = a[1] - penalty;
	cnt[1][1] = 1;
	for (int i = 2; i <= n; i++) {
		// dp[i][0]
		if (dp[i - 1][0] >= dp[i - 1][1]) {
			dp[i][0] =  dp[i - 1][0];
			cnt[i][0] = cnt[i - 1][0];
		} else {
			dp[i][0] = dp[i - 1][1];
			cnt[i][0] = cnt[i - 1][1];
		}
		// dp[i][1]
		if (dp[i - 1][0] + a[i] - penalty >= dp[i - 1][1]) {
			dp[i][1] = dp[i - 1][0] + a[i] - penalty;
			cnt[i][1] = cnt[i - 1][0] + 1;
		} else {
			dp[i][1] = dp[i - 1][1] + a[i];
			cnt[i][1] = cnt[i - 1][1];
		}
	}
	pair<LL, LL> ans;
	if (dp[n][0] > dp[n][1]) {
		ans = make_pair(dp[n][0], cnt[n][0]);
	} else {
		ans = make_pair(dp[n][1], cnt[n][1]);
	}
	return ans;
}

int main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	LL l = 0, r = int(1e18);
	while (r - l > 1) {
		LL mid = (l + r) / 2;
		if (solve(mid).second >= k) {
			l = mid;
		} else {
			r = mid;
		}
	}
	cout << solve(l).first + l * k << '\n';
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...