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 fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair
using namespace std;
typedef long long ll;
const ll inf = (ll)1e9 + 7;
const int N = (int)1e5 + 2;
int n,k,id;
int a[N],pr[N][201],o;
int p[N];
ll mx;
ll dp[N][2];
ll calc (int r,int l) {
return (p[r] - p[l]) * 1ll * (p[n] - p[r]);
}
void compute (int k,int l,int r,int L,int R) {
if (l > r)
return;
int m = (l + r) >> 1;
for (int i = L; i <= min(m,R); i ++) {
ll now = dp[i - 1][(k - 1) & 1] + calc(m,i - 1);
if (now >= dp[m][k & 1])
dp[m][k & 1] = now,pr[m][k] = i;
}
if (mx <= dp[m][k & 1] && m != n)
mx = dp[m][k & 1],id = m;
if (l == r) return;
compute (k,l,m,L,pr[m][k]);
compute (k,m + 1,r,pr[m][k],R);
}
int main () {
scanf ("%d%d", &n,&k);
for (int i = 1; i <= n; i ++) {
scanf ("%d", &a[i]);
p[i] = p[i - 1] + a[i];
}
for (int i = 1; i <= k; i ++)
compute (i,max(id - 1,1),n,max(id - 1,1),n);
printf("%lld\n", mx);
while (id > 0 && k > 0) {
cout << id << " ";
id = pr[id][k] - 1;
k --;
}
}
/**
7 3
4 1 3 4 0 2 3
**/
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:46:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d%d", &n,&k);
^
sequence.cpp:49:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d", &a[i]);
^
# | 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... |