제출 #401989

#제출 시각아이디문제언어결과실행 시간메모리
401989ronnithFeast (NOI19_feast)C++14
41 / 100
1100 ms36952 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = (int)3e5, K = (int)3e5;
int n, k;
int a[N];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> k;
	for (int i = 0;i < n;i ++)
		cin >> a[i];

	auto check = [&](int64_t penalty) -> pair<int, int64_t> {
		vector<vector<int64_t>> dp(n, vector<int64_t>(2, LLONG_MIN));
		vector<vector<int64_t>> count(n, vector<int64_t>(2, INT_MAX));

		if (0 >= a[0] - penalty) {
			dp[0][0] = 0;
			count[0][0] = 0;
		} else {
			dp[0][0] = a[0] - penalty;
			count[0][0] = 1;
		}

		dp[0][1] = a[0] - penalty;
		count[0][1] = 1;
		for (int i = 1;i < n;i ++) {
			if (dp[i - 1][0] + a[i] - penalty > dp[i][0] || (dp[i - 1][0] + a[i] - penalty == dp[i][0] && count[i - 1][0] + 1 < count[i][0])) {
				dp[i][0] = dp[i - 1][0] + a[i] - penalty;
				count[i][0] = count[i - 1][0] + 1;
			}

			if (dp[i - 1][0] > dp[i][0] || (dp[i - 1][0] == dp[i][0] && count[i - 1][0] < count[i][0])) {
				dp[i][0] = dp[i - 1][0];
				count[i][0] = count[i - 1][0];
			}

			if (dp[i - 1][1] + a[i] > dp[i][0] || (dp[i - 1][1] + a[i] == dp[i][0] && count[i - 1][1] < count[i][0])) {
				dp[i][0] = dp[i - 1][1] + a[i];
				count[i][0] = count[i - 1][1];
			}

			if (dp[i - 1][1] + a[i] > dp[i][1] || (dp[i - 1][1] + a[i] == dp[i][1] && count[i - 1][1] < count[i][1])) {
				dp[i][1] = dp[i - 1][1] + a[i];
				count[i][1] = count[i - 1][1];
			}

			if (dp[i - 1][0] + a[i] - penalty > dp[i][1] || (dp[i - 1][0] + a[i] - penalty == dp[i][1] && count[i - 1][0] + 1 < count[i][1])) {
				dp[i][1] = dp[i - 1][0] + a[i] - penalty;
				count[i][1] = count[i - 1][0] + 1;
			}
		}

		return {count[n - 1][0], dp[n - 1][0]};
	};

	int64_t lx = 0, rx = (int64_t)1e15;

	while (lx < rx) {
		int64_t g = lx + (rx - lx) / 2;
		if (check(g).first <= k)
			rx = g;
		else
			lx = g + 1;
	}


	pair<int, int64_t> res = check(lx);
	int64_t ans = res.second + (k - res.first) * lx + lx * k;

	cout << ans << '\n';

	return 0;
}
#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...