# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
446086 | aryan12 | Split the sequence (APIO14_sequence) | C++17 | 2090 ms | 31844 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
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve() {
int n, k;
cin >> n >> k;
int a[n + 1];
a[0] = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
int dp[n + 1][k + 1];
int comefrom[n + 1][k + 1];
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= k; j++) {
dp[i][j] = -1;
comefrom[i][j] = -1;
}
}
dp[0][0] = 0;
int ans = -1, pos;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) {
if(j > i) {
continue;
}
for(int prev = 0; prev < i; prev++) {
if(dp[prev][j - 1] != -1 && dp[i][j] < dp[prev][j - 1] + (a[i] - a[prev]) * (a[n] - a[i])) {
dp[i][j] = max(dp[i][j], dp[prev][j - 1] + (a[i] - a[prev]) * (a[n] - a[i]));
comefrom[i][j] = prev;
}
}
if(j == k) {
if(ans < dp[i][k]) {
pos = i;
}
ans = max(ans, dp[i][k]);
}
}
}
cout << ans << "\n";
while(k != 0) {
cout << pos << " ";
pos = comefrom[pos][k--];
}
cout << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 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... |