# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
892789 | votranngocvy | Feast (NOI19_feast) | C++14 | 27 ms | 5604 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;
#define int long long
const int N = 3e5 + 5;
const int inf = 0x3f3f3f3f3f3f3f3f;
int a[N],pre[N],n,k;
namespace sub1 {
bool check_condition() {
for (int i = 1; i <= n; i++)
if (a[i] < 0) return false;
return true;
}
void solve() {
int ans = 0;
for (int i = 1; i <= n; i++) ans += a[i];
cout << ans << "\n";
}
}
namespace sub2 {
bool check_condition() {
int cnt = 0;
for (int i = 1; i <= n; i++)
if (a[i] < 0) cnt++;
return (cnt == 1);
}
void solve() {
pre[0] = 0;
int pos;
for (int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + a[i];
if (a[i] < 0) pos = i;
}
if (k == 1) cout << max(pre[pos - 1],pre[n] - pre[pos]) << "\n";
else cout << pre[n] - a[pos] << "\n";
}
}
namespace sub5 {
int dp[305][305];
void solve() {
pre[0] = 0;
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
dp[0][0] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
dp[i][j] = dp[i - 1][j];
for (int z = 1; z <= i; z++)
dp[i][j] = max(dp[i][j],dp[z - 1][j - 1] + pre[i] - pre[z - 1]);
}
cout << dp[n][k] << "\n";
}
}
signed main() {
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];
if (sub1::check_condition()) sub1::solve();
else if (sub2::check_condition()) sub2::solve();
else if (n <= 300) sub5::solve();
}
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... |