This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Be nam KHODA
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define int long long
typedef pair<int, int> pii;
const int N = 1e5 + 10, mx = 200 + 10, inf = 2e18;
int n, k;
int a[N], dp[N][2];
int32_t best[N][mx];
void solve(int k, int l = 0, int r = n - 1, int optl = 0, int optr = n - 1){
int mid = (l + r) / 2;
if(l > r) return;
int ans = -1, sum = 0;
for(int i = min(optr, mid); i >= max(1ll, optl); i--){
sum += a[i];
if(sum * sum + dp[i - 1][(k - 1)&1] <= dp[mid][k&1]){
dp[mid][k&1] = sum * sum + dp[i - 1][(k - 1)&1];
ans = i;
}
}
best[mid][k] = ans;
if(l != r){
solve(k, l, mid - 1, optl, ans);
solve(k, mid + 1, r, ans, optr);
}
}
int32_t main(){
cin.tie(0), ios::sync_with_stdio(0);
cin >> n >> k;
++k;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < n; i++)
for(int t = 0; t <= k; t++)
dp[i][t] = inf;
int sum = 0;
for(int i = 0; i < n; i++){
sum += a[i];
dp[i][1] = sum * sum;
best[i][1] = -1;
}
for(int i = 2; i <= k; i++)
solve(i);
sum = (sum * sum - dp[n - 1][k&1]) / 2;
cout << sum << endl;
int i = n - 1, kk = k;
vector<int> ans;
while(kk != 1){
ans.push_back(best[i][kk]);
i = best[i][kk] - 1;
kk--;
}
reverse(all(ans));
for(auto & v: ans)
cout << v << ' ';
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... |