This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//khodaya khodet komak kon
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimise ("ofast")
#pragma GCC optimise("unroll-loops")
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 100000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000000000000000;
const ll LOG = 25;
int n, k, a[N], ps[N], opt[N][210];
ll dp[N][2];
ll cost (int l, int r){
ll x = ps[r] - ps[l - 1];
return x * x;
}
int solve(int l, int r, int lq, int rq, int x, int f){
if (rq < lq) return 0;
int md = (lq + rq) >> 1;
for (int i = l; i < min(r + 1, md); i++){
if (dp[md][f] > dp[i][f ^ 1] + cost(i + 1, md)){
dp[md][f] = dp[i][f ^ 1] + cost(i + 1, md);
opt[md][x] = i;
}
}
//cout << opt[md][x] << '\n';
if (lq == rq) return 0;
solve(l, opt[md][x], lq, md - 1, x,f);
solve(opt[md][x], r, md + 1, rq, x,f);
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> a[i];
ps[i] = ps[i - 1] + a[i];
}
bool f = 0;
for (int i = 1; i <= n; i++){
dp[i][1] = ps[i] * ps[i];
}
for (int i = 2; i <= k + 1; i++){
for (int j = 0; j < N; j++) dp[j][f] = INF;
solve(1, n, 1, n, i, f);
f ^= 1;
}
cout << (ps[n] * 1ll * ps[n] - dp[n][f ^ 1]) / 2<< '\n';
vector<int> ans;
int Now = n;
for (int i = k + 1; i >= 2; i--){
ans.pb(opt[Now][i]);
Now = opt[Now][i];
}
reverse(all(ans));
for (auto u:ans) cout << u << ' ';
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
Compilation message (stderr)
sequence.cpp:8:0: warning: ignoring #pragma GCC optimise [-Wunknown-pragmas]
#pragma GCC optimise ("ofast")
sequence.cpp:9:0: warning: ignoring #pragma GCC optimise [-Wunknown-pragmas]
#pragma GCC optimise("unroll-loops")
sequence.cpp: In function 'long long int solve(long long int, long long int, long long int, long long int, long long int, long long int)':
sequence.cpp:44:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# | 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... |