This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const ll is_query = -9223372036854775807;
struct line{
ll m,c,id = 0;//y = mx+c
line(ll a,ll b,ll d){m = a;c = b;id=d;}
ll eval(ll x){return m*x+c;}
ld interx(line l) {return (ld)((l.c - c)/(m - l.m));}
mutable function<const line*()> succ; //succesor
bool operator<(const line &r)const{
if(r.c != is_query)return r.m > m;
const line* s = succ();
if (!s) return 0;
ll x = r.m;
return 1.0L * c - s->c < 1.0L *(s->m - m) * x; //is x greater than the intersection point of s and succ?
//if yes then we move forward ez
}
};
vector<deque<line>>hulls(201);
void add(int i,line l){
if(hulls[i].empty()){hulls[i].pb(l);return;}
if(hulls[i].front().m == l.m){
if(l.c < hulls[i].front().c)hulls[i].front();
else return;
}
while(sz(hulls[i])>1){
line a = hulls[i][0];
line b = hulls[i][1];
if((l.c - a.c) *(b.m - a.m)>= (a.c - b.c)*(a.m - l.m))hulls[i].pop_front();
else break;
}
hulls[i].push_front(l);
}
PII query(int i,ll x){
while(sz(hulls[i]) > 1 && hulls[i].back().eval(x) < hulls[i][sz(hulls[i])-2].eval(x))hulls[i].pop_back();
return {hulls[i].back().eval(x),hulls[i].back().id};
}
int main()
{
owo
int n,k;
cin>>n>>k;
vector<ll>a(n);
for(int i=0;i<n;i++)cin>>a[i];
VII dp(n,vector<ll>(k+1)),par(n,vector<ll>(k+1));
add(0,line(0,0,-1));
vector<ll>p = a;
for(int i=1;i<n;i++)p[i]+=p[i-1];
for(int i=0;i<n-1;i++){
for(int j=k;j>=1;j--){
if(hulls[j-1].empty())continue;
PII v = query(j-1,p[n-1]-p[i]);
dp[i][j] = v.fi + p[i]*(p[n-1] - p[i]);
par[i][j] = v.se;
//cout<<i<<" "<<j<<" "<<v.se<<'\n';
add(j,line(-p[i],dp[i][j],i));
}
}
ll ans = 0;
for(int i=0;i<n-1;i++){ans = max(ans,dp[i][k]);}
//cout<<'\n';
cout<<ans<<'\n';
vector<int>seq;
for(int i=0;i<n;i++){
if(dp[i][k] == ans){
int cur = i;
for(int j=k;j>=1;j--){
seq.pb(cur);
if(cur == -1)break;
cur = par[cur][j];
}
break;
}
}
reverse(all(seq));
for(int x:seq)cout<<x+1<<" ";
}
# | 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... |