This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "FEAST"
#define ll long long
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maximize(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)3e5 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
int n, k;
int a[N];
namespace sub3 {
bool valid() {
return k == 1;
}
void solve() {
ll sum = 0, minPre = 0, ans = 0;
FOR(i, 1, n) {
sum += a[i];
maximize(ans, sum - minPre);
minimize(minPre, sum);
}
cout << ans << "\n";
}
}
namespace sub1 {
bool valid() {
return *min_element(a + 1, a + n + 1) >= 0;
}
void solve() {
ll ans = 0;
FOR(i, 1, n) ans += a[i];
cout << ans << "\n";
}
}
namespace sub2 {
bool valid() {
bool appeared = 0;
FOR(i, 1, n) {
if (a[i] < 0 && appeared) return false;
if (a[i] < 0) appeared = 1;
}
return true;
}
void solve() {
ll ans = 0;
FOR(i, 1, n) ans += a[i];
ans -= *min_element(a + 1, a + n + 1);
cout << ans << "\n";
}
}
namespace sub5 {
const int N = 303;
ll dp[N][N];
ll sum[N];
bool valid() {
return n <= 300;
}
void solve() {
FOR(i, 1, n) sum[i] = sum[i - 1] + a[i];
memset(dp, -0x3f, sizeof(dp));
dp[0][0] = 0;
FOR(i, 1, n) FOR(j, 1, min(k, i)) {
ll maxDP = dp[j - 1][j - 1];
FOR(x, j, i) {
maximize(dp[i][j], maxDP + (sum[i] - sum[x - 1]));
maximize(maxDP, dp[x][j - 1]);
}
}
ll ans = 0;
FOR(i, 1, n) FOR(j, 1, min(k, i)) maximize(ans, dp[i][j]);
cout << ans << "\n";
}
}
namespace sub6 {
const int N = (int)2e3 + 3;
ll dp[N][N][2];
bool valid() {
return n <= 2000;
}
void solve() {
memset(dp, -0x3f, sizeof(dp));
dp[0][0][0] = dp[0][0][1] = 0;
ll ans = 0;
FOR(i, 1, n) FOR(j, 1, min(i, k)) {
dp[i][j][1] = max({1LL * a[i], dp[i - 1][j][1] + a[i], dp[i - 1][j - 1][0] + a[i]});
dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j][1]);
maximize(ans, max({dp[i][j][0], dp[i][j][1]}));
//cout << i << " " << j << " " << dp[i][j][0] << " " << dp[i][j][1] << "\n";
}
cout << ans << "\n";
}
}
void main_code() {
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
if (sub3::valid()) sub3::solve();
else if (sub1::valid()) sub1::solve();
else if (sub2::valid()) sub2::solve();
else if (sub5::valid()) sub5::solve();
else if (sub6::valid()) sub6::solve();
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0;
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); cout << "\n"; }
}
/* Lak lu theo dieu nhac */
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:146:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |