#include <bits/stdc++.h>
using namespace std;
int main() {
int n, K;
cin >> n >> K;
vector<int> arr(n);
for(int &a : arr) cin >> a;
vector<int> prefix(n+1, 0);
for(int i = 0; i < n; i++) {
prefix[i+1] = prefix[i] + arr[i];
}
vector<vector<int>> dp(n+1, vector<int>(K+1, 0));
vector<vector<int>> prev(n+1, vector<int>(K+1, -1));
for(int k = 1; k <= K; k++) {
for(int i = k; i <= n; i++) {
for(int p = 0; p < i; p++) {
if((dp[p][k-1] + (prefix[i] - prefix[p]) * prefix[p])>dp[i][k]){
prev[i][k]=p;
dp[i][k] = dp[p][k-1] + (prefix[i] - prefix[p]) * prefix[p];
}
}
}
}
vector<int>splits;
cout<<dp[n][K]<<"\n";
int parent=prev[n][K];
int k=K;
splits.push_back(parent);
while(prev[parent][k]!=-1){
k--;
parent=prev[parent][k];
splits.push_back(parent);
}
reverse(splits.begin(),splits.end());
for(int &res:splits){
cout<<res<<" ";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Runtime error |
0 ms |
348 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
548 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
191 ms |
2896 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2058 ms |
12124 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |