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];
int cnt,o;
int p[N];
ll mx;
ll dp[N][201];
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;
int opt = -1;
for (int i = L; i <= min(m,R); i ++) {
ll now = dp[i - 1][(k - 1)] + calc(m,i - 1);
if (now >= dp[m][k ])
dp[m][k ] = now,opt = i;
}
pr[m][k] = opt;
compute (k,l,m - 1,L,opt);
compute (k,m + 1,r,opt,R);
}
int main () {
scanf ("%d%d", &n,&k);
o = k;
for (int i = 1; i <= n; i ++) {
scanf ("%lld", &a[i]);
p[i] = p[i - 1] + a[i];
}
for (int i = 1; i <= n; i ++) {
dp[i][1] = calc(i,0);
pr[i][1] = 1;
}
for (int i = 2; i <= k; i ++)
compute (i,i,n,i,n);
for (int i = 1; i < n; i ++) {
if (mx <= dp[i][k])
mx = max(mx,dp[i][k]),id = i;
}
printf("%lld\n", mx);
deque<int> dq;
while (id > 0 && k > 0) {
if (cnt == o)
break;
dq.push_front(id);
id = pr[id][k] - 1;
k --;
cnt ++;
}
for (int i = 0; i < (int)dq.size(); i ++)
printf("%d ", dq[i]);
}
/**
7 3
4 1 3 4 0 2 3
**/
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:50:37: warning: format '%lld' expects argument of type 'long long int*', but argument 2 has type 'int*' [-Wformat=]
scanf ("%lld", &a[i]);
^
sequence.cpp:45: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:50:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%lld", &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... |