# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1106170 | manhlinh1501 | Feast (NOI19_feast) | C++17 | 33 ms | 5080 KiB |
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>
using namespace std;
using i64 = long long;
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
const int MAXN = 3e5 + 5;
const int oo32 = 1e9 + 5;
const i64 oo64 = 1e18 + 5;
int N, K;
i64 a[MAXN];
namespace subtask1 {
bool is_subtask() {
for(int i = 1; i <= N; i++) {
if(a[i] < 0) return false;
}
return true;
}
void solution() {
if(is_subtask() == false) return;
i64 ans = accumulate(a + 1, a + N + 1, 0LL);
cout << ans;
exit(0);
}
}
namespace subtask2 {
bool is_subtask() {
int c = 0;
for(int i = 1; i <= N; i++)
c += (a[i] < 0);
return c <= 1;
}
void solution() {
if(is_subtask() == false) return;
i64 tot = accumulate(a + 1, a + N + 1, 0LL);
i64 ans = tot;
for(int i = 1; i <= N; i++) ans = max(ans, tot - a[i]);
cout << ans;
exit(0);
}
}
namespace subtask3 {
bool is_subtask() {
return K == 1;
}
i64 dp[MAXN];
void solution() {
if(is_subtask() == false) return;
for(int i = 1; i <= N; i++) dp[i] = max(dp[i - 1], 0LL) + a[i];
cout << *max_element(dp + 1, dp + N + 1);
exit(0);
}
}
namespace subtask {
void solution() {
for(int i = 1; i <= N; i++) a[i] += a[i - 1];
vector<vector<i64>> dp(N + 1, vector<i64>(K + 1, -oo64));
vector<i64> maxx(N + 1, 0);
dp[0][0] = 0;
for(int i = 1; i <= N; i++) maxx[i] = -a[i];
for(int j = 1; j <= K; j++) {
for(int i = 1; i <= N; i++)
maximize(dp[i][j], maxx[i - 1] + a[i]);
for(int i = 1; i <= N; i++) maxx[i] = max(maxx[i - 1], dp[i][j]);
for(int i = 1; i <= N; i++) maxx[i] = max(maxx[i - 1], maxx[i] - a[i]);
}
i64 ans = 0;
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= K; j++)
maximize(ans, dp[i][j]);
}
cout << ans;
}
}
signed main() {
#define task "code"
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> K;
for(int i = 1; i <= N; i++) cin >> a[i];
subtask1::solution();
subtask3::solution();
subtask2::solution();
subtask::solution();
return (0 ^ 0);
}
Compilation message (stderr)
# | 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... |