이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define lc id<<1
#define rc id<<1^1
#define nmax 100008
const long long inf = 1e18;
using namespace std;
typedef pair<long long, long long> pii;
long long n, k, row, a[nmax], f[nmax];
long long sum[nmax], dp[208][nmax], trace[208][nmax];
void dq(long long l, long long r, long long u, long long v){
if (l > r)
return;
long long mid = (l + r) >> 1, sp = -1;
dp[row][mid] = inf;
for (long long i = u; i <= mid && i <= v; i++)
if (dp[row][mid] > dp[row - 1][i - 1] + sum[mid] - sum[i - 1] - 1LL * (f[mid] - f[i - 1]) * f[i - 1]){
dp[row][mid] = dp[row - 1][i - 1] + sum[mid] - sum[i - 1] - 1LL * (f[mid] - f[i - 1]) * f[i - 1];
sp = i;
}
trace[row][mid] = sp;
dq(l, mid - 1, u, sp);
dq(mid + 1, r, sp, v);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (long long i = 1; i <= n; i++){
cin >> a[i];
f[i] = f[i - 1] + a[i];
sum[i] = sum[i - 1] + a[i] * f[i - 1];
dp[0][i] = inf;
}
k++;
dp[0][0] = 0;
for (row = 1; row <= k; row++)
dq(row, n, row, n);
cout << sum[n] - dp[k][n] << '\n';
for (long long j = k, i = n; j > 0; j--){
i = trace[j][i] - 1;
if (i)
cout << i << ' ';
}
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... |