#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 &temp : oriVals) {
// 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]);
// }
// }
// 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();
static int dp[2005][2005][2] = {0};
if (newN < 1) return -1;
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]);
}
}
cout << max(dp[newN][k][1], dp[newN][k][0]) << '\n';
// srand(time({}));
// for (int i = 0; i < 100; ++i) {
// cout << "Test: " << i + 1 << '\n';
// int n = rand() % 100 + 1, k = rand() % 100 + 1;
// vector<int> oriVals(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... |