This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
#define REP(i,n) for (int i = 0; i<n; ++i)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__)
template<typename T> void _do(T && x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y){cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif // BALBIT
#define SZ(x) (int)(x.size())
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
struct line{
ll m,b;
int id;
};
double its(line a, line b){
if (a.m == b.m) return 1e18+1029;
return (b.b-a.b) / (double)(a.m-b.m);
}
list<line>dq[203];
ll dp[100005][203];
int frm[100005][203];
ll ps[100005];
signed main(){
IOS();
int n,K; cin>>n>>K; ++K;
assert(K >= 2);
for (int i = 0; i<n; ++i) {
cin>>ps[i+1]; ps[i+1] += ps[i];
}
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
for (int i=1;i<=n;++i){
for (int k=1; k<=K; ++k) {
for (int j = 0; j<i; ++j) {
ll tt = dp[j][k-1] + (ps[i] - ps[j]) * (ps[i] - ps[j]);
if (tt <= dp[i][k]) {
frm[i][k] = j; dp[i][k] = tt;
}
}
// bug(i,k,dp[i][k]);
}
}
// bug(dp[n][K]);
cout<<(ps[n]*ps[n] - dp[n][K]) / 2<<endl;
int at = frm[n][K];
for (int k = K-1; k>0; --k) {
cout<<at<<' ';
at = frm[at][k];
}
cout<<endl;
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |