#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
// int code(int oriN, int k, vector<int> &oriVals) {
// vector<int> vals;
// int cur = 0;
// for (int i = 0; i < oriN; ++i) {
// int temp = oriVals[i];
// if (cur == 0 || ((temp < 0) && (cur < 0)) || ((temp > 0) && (cur > 0))) cur += temp;
// else {
// vals.push_back(cur);
// cur = temp;
// }
// }
// if (cur) vals.push_back(cur);
// int newN = vals.size();
// static int dp[2005][2005][2] = {0};
// for (int i = 1; i <= newN; ++i) {
// for (int j = 1; j <= k; ++j) {
// dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 1][j - 1][0]) + vals[i - 1];
// dp[i][j][0] = max(dp[i - 1][j][1], dp[i - 1][j][0]);
// }
// }
// if (newN == 1 && max(dp[newN][k][1], dp[newN][k][0]) != vals[0]) return -1;
// return max(dp[newN][k][1], dp[newN][k][0]);
// }
// int bruteforce(int oriN, int k, vector<int> &oriVals) {
// int res = 0;
// for (int &i : oriVals) res += i;
// return res;
// }
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int oriN, k; cin >> oriN >> k;
vector<int> vals;
int cur = 0;
for (int i = 0; i < oriN; ++i) {
int temp; cin >> temp;
if (cur == 0 || ((temp < 0) && (cur < 0)) || ((temp > 0) && (cur > 0))) cur += temp;
else {
vals.push_back(cur);
cur = temp;
}
}
if (cur) vals.push_back(cur);
int newN = vals.size();
auto check = [&](int penalty) {
vector<vector<pair<int, int>>> dp(newN + 1, vector<pair<int, int>>(2));
dp[1][0] = {0, 0};
dp[1][1] = {vals[0] - penalty, 1};
for (int i = 2; i <= newN; ++i) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(make_pair(dp[i - 1][0].first + vals[i - 1] - penalty, dp[i - 1][0].second + 1), make_pair(dp[i - 1][1].first + vals[i - 1], dp[i - 1][1].second));
}
return max(dp[newN][0], dp[newN][1]);
};
int low = 0, high = 1e18;
while (low < high) {
int mid = (low + high + 1) / 2;
if (check(mid).second >= k) low = mid;
else high = mid - 1;
}
cout << (check(low).first + low * k) << '\n';
// srand(time({}));
// for (int i = 0; i < 100; ++i) {
// cout << "Test: " << i + 1 << '\n';
// int n = rand() % 100 + 1, k = rand() % n + 1;
// vector<int> oriVals(n);
// cout << "n: " << n << "\nk: " << k << '\n';
// cout << "Vals: ";
// for (int i = 0; i < n; ++i) oriVals[i] = rand() % 100'000, cout << oriVals[i] << ' ';
// cout << '\n';
// int codeRes = code(n, k, oriVals), bruteforceRes = bruteforce(n, k, oriVals);
// if (codeRes == bruteforceRes) cout << "Test Success.\n";
// else {
// cout << "Test Failed.\n";
// cout << "Code: " << codeRes << '\n';
// cout << "Bruteforce: " << bruteforceRes << '\n';
// break;
// }
// }
return 0;
}
# | 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... |