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 TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
long long dp[205][N], f[N];
int trace[205][N];
int n, k;
int a[N];
void read() {
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
}
long long sum(int l, int r) {
if (l > r) return 0;
return f[r] - f[l - 1];
}
long long cost(int l, int r) {
return -(1LL * sum(l, r) * sum(r + 1, n));
}
void calDp(int g, int l, int r, int optl, int optr) {
if (l > r) return;
int best = optl;
int mid = (l + r) >> 1;
for(int i = optl; i <= min(mid, optr); i++) {
if (minimize(dp[g][mid], dp[g - 1][i - 1] + cost(i, mid))) {
trace[g][mid] = i - 1;
// cerr << g << ' ' << mid << ' ' << -dp[g][mid] << endl;
best = i;
}
}
calDp(g, l, mid - 1, optl, best);
calDp(g, mid + 1, r, best, optr);
}
void solve() {
for(int i = 1; i <= n; i++) f[i] = f[i - 1] + a[i];
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0;
++k;
for(int i = 1; i <= k; i++) calDp(i, 1, n, 1, n);
cout << -dp[k][n] << endl;
int j = k, i = n;
vector<int> c;
while(i) {
i = trace[j][i];
if (i == 0) break;
c.push_back(i);
--j;
}
reverse(ALL(c));
for(int &x: c) cout << x << ' ';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// if (fopen(TASK".inp", "r")) {
// freopen(TASK".inp", "r", stdin);
// freopen(TASK".out", "w", stdout);
// }
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
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... |