#include <bits/stdc++.h>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> pref(n + 1);
vector<int> suf(n + 2);
for(int i = 1; i <= n; i++) {
pref[i] = pref[i - 1] + a[i];
}
for(int i = n; i > 0; i--) {
suf[i] = suf[i + 1] + a[i];
}
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= m; j++) {
dp[i][j] = 0;
}
}
vector<vector<int>> P(n + 1, vector<int>(m + 1));
for(int i = 1; i <= n; i++) {
dp[i][1] = (pref[i]) * (suf[i + 1]);
P[i][1] = i;
}
for(int i = 1; i <= n; i++) {
for(int j = 2; j < i; j++) {
for(int k = 1; k < i; k++) {
//dp[i][j] = max(dp[i][j], dp[k][j - 1] + (pref[i] - pref[k]) * suf[i + 1]);
int s = dp[k][j - 1] + (pref[i] - pref[k]) * suf[i + 1];
if(s > dp[i][j]) {
P[i][j] = k;
dp[i][j] = s;
}
}
}
}
int ans = 0;
for(int i = m + 1; i <= n; i++) {
ans = max(ans, dp[i][m]);
}
int x = 0;
for(int i = m + 1; i <= n; i++) {
if(ans == dp[i][m]) {
x = i;
}
}
vector<int> R;
R.push_back(x);
while(m > 1) {
int u = P[x][m];
R.push_back(u);
x = u;
m--;
}
reverse(R.begin(), R.end());
cout << ans << "\n";
for(auto it : R) {
cout << it << " ";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
329 ms |
1040 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2043 ms |
1628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2047 ms |
12628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |