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;
dp[mid][k&1] = inf;
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;
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];
int sum = 0;
for(int i = 0; i < n; i++){
sum += a[i];
dp[i][1] = sum * sum;
}
for(int i = 2; i <= k; i++)
solve(i);
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));
sum *= sum;
int ptr = 0, ss = 0;
for(int i = 0; i < n; i++){
ss += a[i];
if(ptr < k and i == ans[ptr] - 1){
sum -= ss * ss;
ss = 0;
ptr++;
}
}
sum -= ss * ss;
cout << sum / 2 << endl;
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... |