이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int K = 207;
const ll INF = 8e18;
int n, k, a[N], s[N];
ll dp[2][N];
int trace[K][N];
void Calc(int k, int l, int r, int from, int to){
if (l > r)
return;
int mid = (l + r) >> 1;
for(int i = from; i <= min(mid - 1, to); i++){
ll cost = (ll) (s[mid] - s[i]) * (s[n] - s[mid]);
if (dp[1][mid] < dp[0][i] + cost){
dp[1][mid] = dp[0][i] + cost;
trace[k][mid] = i;
}
}
Calc(k, l, mid - 1, from, trace[k][mid]);
Calc(k, mid + 1, r, trace[k][mid], to);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
for(int i = 0; i < 2; i++)
for(int j = 0; j <= n; j++)
dp[i][j] = -INF;
dp[1][0] = 0;
for(int i = 1; i <= k + 1; i++){
for(int j = 0; j <= n; j++){
dp[0][j] = dp[1][j];
dp[1][j] = -INF;
}
Calc(i, 1, n, 0, n - 1);
}
cout << dp[1][n] << '\n';
int cur = n;
for(int i = k + 1; i > 1; i--){
cur = trace[i][cur];
cout << cur << ' ';
}
return 0;
}
# | 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... |