#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define pii pair <ll , ll>
#define fi first
#define se second
using namespace std;
const ll INF = 1e18;
const int maxn = 1e5 + 2;
const int maxk = 2e2 + 2;
struct line
{
ll a , b , p , trace;
bool operator < (line o) {return a < o.a;}
bool operator < (int k) {return p < k;}
};
struct Convex_Hull_Trick
{
ll div(ll a , ll b)
{
return a/b - ((a^b) < 0 && a%b);
}
void isect(line &l , line y)
{
if(l.a == y.a) l.p = (l.b > y.b)? INF : -INF;
else l.p = div(y.b - l.b , l.a - y.a);
}
vector <line> x;
void add(line l)
{
if(!x.empty()) isect(x.back() , l);
while(x.size() > 1 && x[x.size()-2].p >= x.back().p)
{
x.pop_back();
isect(x.back() , l);
}
x.push_back(l);
}
pii query(int v)
{
line l = *lower_bound(x.begin() , x.end() , v);
return (pii){v*l.a + l.b , l.trace};
}
} cht[maxk];
int n , k , a[maxn];
ll pre[maxn] , suf[maxn];
int path[maxn][maxk];
void solve()
{
cin >> n >> k; k++;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
pre[i] = pre[i-1] + a[i];
}
for(int i = n; i >= 1; i--)
{
suf[i] = suf[i+1] + a[i];
}
cht[0].add({0 , 0 , INF , 0});
ll ans = 0;
for(int i = 1; i <= n; i++)
{
for(int z = min(i , k); z >= 1; z--)
{
pii tmp = cht[z-1].query(-suf[i+1]);
ll dp = tmp.fi + pre[i]*suf[i+1];
path[i][z] = tmp.se;
cht[z].add({pre[i] , dp , INF , i});
if(i == n && z == k) ans = dp;
}
}
cout << ans << '\n';
vector <int> qq;
int curi = n;
int curk = k;
while(curk > 1)
{
curi = path[curi][curk];
curk--;
qq.push_back(curi);
}
reverse(qq.begin() , qq.end());
for(int x: qq) cout << x << ' ';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
//freopen("inp.txt" , "r" , stdin);
//freopen("out.txt" , "w" , stdout);
solve();
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... |