This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
author: Saksham_2000
"It's a slow process,
but quitting won't speed it up"
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define cld complex<ld>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vld vector<ld>
#define vvld vector<vector<ld>>
#define vpll vector<pll>
#define vcld vector<cld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define eb emplace_back
#define PI acos(-1)
#define endl "\n"
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end());
#define mset(a,val) memset(a,val,sizeof(a));
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define popcount(x) __builtin_popcountll(x)
#define sz(x) ((ll)x.size())
#define FOR(i,a,b) for(ll i=a;i<=b;i++)
#define FORR(i,a,b) for(ll i=a;i>=b;i--)
const ll N = 2e5 + 5;
const ll M = 1000000007;
const ll MM = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
ll begtime = clock();
#define end_routine() cerr << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
void __print(const complex<long double> &x) {cerr << '{'; __print(x.real()); cerr << ','; __print(x.imag()); cerr << '}';}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
template<typename T, typename F>
void chmax( T &a, F b) {
if (b > a)a = b;
}
template<typename T, typename F>
void chmin( T &a, F b) {
if (b < a)a = b;
}
ll powM(ll a, ll b, ll m)
{
if (a <= 0)return 0;
a %= m;
ll ans = 1LL;
while (b)
{
if (b & 1)ans = ans * a % m;
//ans = mulmod(ans, a, m);
a = a * a % m;
//a = mulmod(a, a, m);
b >>= 1;
}
return ans;
}
ll poww(ll a, ll b)
{
if(b<0)return 0;
ll ans = 1;
while (b)
{
if (b & 1)ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
void OJ() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
struct seg{ ld x; ll m,b,id;};
vector<seg>hull;
// for maxima, lines 2b inserted in non-dec order, for minima-> non inc.
void insert_to_hull(ll m,ll b,ll id){
while(hull.size()){
seg s=hull.back();
if(s.b+s.m*s.x>b+m*s.x){ // for minima, '<' sign will come.
if(s.m-m)hull.push_back({(b-s.b)/(ld)(s.m-m),m,b,id});
return;
}
hull.pop_back();
}
hull={{LLONG_MIN,m,b,id}};
}
pll query_hull(ll x){
seg s=*--upper_bound(hull.begin(),hull.end(),x,[](ll a,seg b){return a<b.x;});
return {s.b+s.m*x,s.id};
}
int main() {
IOS;
// OJ();
ll n;cin>>n;
ll k;cin>>k;
vl pr(n+1);
vvl dp(n+1,vl(k+1,-1e18));
vvl prv(n+1,vl(k+1,-1));
for(ll i=1;i<=n;i++){
ll x;cin>>x;
pr[i]=pr[i-1]+x;
}
for(ll i=0;i<=n;i++){
dp[i][0]=0;
}
for(ll kk=1;kk<=k;kk++){
hull.clear();
insert_to_hull(pr[n]+pr[0],-pr[n]*pr[0]+dp[0][kk-1],0);
for(ll i=1;i<=n;i++){
// dp[j][kk-1] will be used here. (j<i)
auto val=query_hull(pr[i]);
dp[i][kk]=val.ff-pr[i]*pr[i],prv[i][kk]=val.ss;
insert_to_hull(pr[i]+pr[n],-pr[n]*pr[i]+dp[i][kk-1],i);
}
}
ll ans=-1e18,st=-1;
FOR(i,1,n){
if(ans<dp[i][k]){
ans=dp[i][k];
st=i;
}
}
vl recover;
ll cur=k;
while(cur>0&&st!=-1){
recover.pb(st);
st=prv[st][cur--];
}
reverse(recover.begin(),recover.end());
cout<<ans<<endl;
for(auto d:recover){
cout<<d<<" ";
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'void OJ()':
sequence.cpp:143:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:144:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
144 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |