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 <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <string.h>
#include <functional>
// #define int long long
#define size() size() * 1ll
#define all(x) (x).begin(), (x).end()
#define allr(x, sz) (x) + 1, (x) + 1 + sz
#define pb push_back
#define pii pair<int, int>
#define MASK(x) (1LL << (x))
#define BIT(x,i) (((x) >> (i)) & 1)
#define numbit(x) __builtin_popcountll(x)
using namespace std;
const int N = 1e5 + 5;
const int M = 1e3 + 1;
const long long mod = 1e9 + 7;
const long long oo = 1e18 + 7;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;
template<class t>
bool mini(t &x,t y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
template<class t>
bool maxi(t &x,t y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
void file() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
return ;
}
const int K = 205;
int n, k;
int a[N], trace[N][K];
ll dp[N][2], suf[N];
void dnc(int z, int l, int r, int u, int v) {
if (l > r) return ;
int mid = (l + r) >> 1;
int g = u;
for (int i = u; i <= v; i++) {
if (maxi(dp[mid][z & 1], dp[i][(z & 1) ^ 1] + (suf[i] - suf[mid]) * suf[mid])) {
trace[mid][z] = i;
g = i;
}
}
dnc(z, l, mid - 1, u, g);
dnc(z, mid + 1, r, g, v);
}
void solve(int test = -1) {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
n++;
k++;
for (int i = n; i >= 1; i--) {
suf[i] = suf[i + 1] + a[i];
}
for (int z = 1; z <= k; z++) {
for (int i = 1; i <= n; i++) dp[i][z & 1] = -1e18;
dnc(z, z + 1, n, z, n);
}
cout << dp[n][k & 1] << '\n';
while (k > 1) {
n = trace[n][k];
cout << n - 1 << ' ';
k--;
}
}
int32_t main() {
file();
int T = 1;
// cin >> T;
for (int i = 1; i <= T; i++) {
solve(i);
}
return 0;
}
/*
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... |