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>
#define pll pair<ll, ll>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ar array
#define vi vector
#define bit(i,x) ((x >> i) & 1ll)
#define all(x) x.begin(),x.end()
#define low(l,r,x) x.begin()+l,x.begin()+r
#define len(l,r) (r-l+1)
#define PI acos(-1.0)
template <class T> void setmin(T &a, T b) { if(b < a) a = b; }
template <class T> void setmax(T &a, T b) { if(b > a) a = b; }
const long long mod = 998244353, N = 1e5, INF = 1e18, Log = 19, MASK = 1 << 19;
using namespace std;
vector<ll> dp_before, dp_cur;
ll pfs[N+5] , perf[N+5];
int n , k;
int trace[201][N+5];
ll C(ll i , ll j) {
ll k = pfs[j] - pfs[i-1];
ll res = k*k;
res -= (perf[j] - perf[i-1]);
return res;
}
void compute(ll x,ll l,ll r, ll optl, ll optr) {
if(l > r) return;
ll mid = (l+r) >> 1;
pll best = {INF,-1};
for(int i = optl; i <= min(mid,optr);i++) {
// best = min(best, {dp_before[i-1] + C(i,mid),i});
if(dp_before[i-1] + C(i,mid) <= best.fi) {
best.fi = dp_before[i-1] + C(i,mid);
best.se = i;
}
}
if(best.se == -1) return;
dp_cur[mid] = best.fi;
trace[x][mid] = best.se - 1;
int opt = best.se;
compute(x,l,mid-1,optl,opt);
compute(x,mid+1,r,opt,optr);
}
void kazuha() {
cin >> n >> k;
ll a[n+5];
pfs[0] = 0;
perf[0] = 0;
for(int i = 1; i <= n;i++) {
cin >> a[i];
pfs[i] = pfs[i-1] + a[i];
perf[i] = perf[i-1] + a[i]*a[i];
}
ll ans = pfs[n]*pfs[n] - perf[n];
dp_cur.assign(n+5,INF);
dp_before.assign(n+5,INF);
dp_before[0] = 0;
dp_cur[0] = 0;
for(int i = 1; i <= k+1;i++) {
compute(i,1,n,1,n);
dp_before = dp_cur;
}
ans = (ans - dp_before[n])/2;
cout << ans << '\n';
ll i = k+1, j = n;
vi<ll> v;
while(trace[i][j]) {
v.push_back(trace[i][j]);
j = trace[i--][j];
}
reverse(all(v));
for(auto k : v) {
cout << k << " ";
}
cout << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("permutation.cpp", "r", stdin);
// freopen("permutation.cpp", "w", stdout);
long long mitsu = 1;
// cin >> mitsu;
for(long long seele = 0; seele < mitsu;seele++) {
// cout << "Case " << seele << " :";
kazuha() ;
}
}
# | 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... |