This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
O(NK) ....?
*/
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
struct fr { // p/q -> 진짜로 만들어야만 했을까
ll p, q;
fr(ll p = 0, ll q = 1):p(p),q(q){}
inline bool operator < (const fr &A) const {return (q*A.q>0)?(p*A.q<q*A.p):(p*A.q>q*A.p);}
inline bool operator == (const fr &A) const {return p*A.q==q*A.p;}
inline bool operator <= (const fr &A) const {return (*this)<A||(*this)==A;}
inline bool operator > (const fr &A) const {return !((*this)<=A);}
inline bool operator < (const ll &A) const {return (*this)<fr(A,1);}
inline bool operator == (const ll &A) const {return (*this)==fr(A,1);}
inline bool operator > (const ll &A) const {return (*this)>fr(A,1);}
};
struct line {
ll a, b;
int idx;
line(ll a=0, ll b=0, int idx=0):a(a),b(b),idx(idx){}
fr operator * (const line &A) const {return fr(b-A.b,A.a-a);}
};
int n, m;
int arr[100010];
ll psum[100010];
ll dyn[2][100010];
int via[210][100010];
line st[100010];
int key = 0;
void pop(){key--;}
void push(line a) {
while(key>1) {
if (st[key-1].a==a.a) {
if (st[key-1].b<a.b) pop();
else return;
continue;
}
if (st[key-1]*st[key-2]<=st[key-2]*a) pop();
else break;
}
st[key++] = a;
}
int s;
pair<ll,int> getval(ll a) {
int e = key-2, m;
s = max(min(s,key-2),0);
while(s<=e) {
if (st[s]*st[s+1]<=a) return pair<ll,int>((a*st[s].a+st[s].b),st[s].idx);
s++;
}
return pair<ll,int>(a*st[s].a+st[s].b,st[s].idx);
}
void clear() {key=0;}
void process() {
int i, j, k;
for (i=1;i<=n;i++) dyn[0][i] = psum[i]*(psum[n]-psum[i]);
for (i=2;i<=m;i++) {
clear();
for (j=1;j<i;j++) {push(line(-psum[j],dyn[0][j],j));dyn[1][j]=0;}
for (j=i;j<n;j++) {
pair<ll,int> pli = getval(psum[n]-psum[j]);
dyn[1][j] = pli.first+psum[j]*(psum[n]-psum[j]);
via[i][j] = pli.second;
push(line(-psum[j],dyn[0][j],j));
}
for (j=1;j<=n;j++) dyn[0][j] = dyn[1][j];
}
ll maxi = 0;
int idx = -1;
for (i=1;i<n;i++) {
if (maxi<=dyn[0][i]) {
maxi = dyn[0][i];
idx = i;
}
}
printf("%lld\n",maxi);
for (i=m;i>0;idx=via[i--][idx]) printf("%d ",idx);
printf("\n");
}
void input() {
int i;
scanf("%d %d",&n,&m);
for (i=1;i<=n;i++) {
scanf("%d",&arr[i]);
psum[i] = psum[i-1]+arr[i];
}
}
int main() {
input();
process();
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... |