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 <vector>
#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
using namespace std;
ll dp[100001][2];
int prevs[100001][201];
deque<vector<ll>> sus;
bool sussy(ll a, ll b, ll mi, ll ci){
return (sus[a][1] - ci) * (sus[a][0] - sus[b][0]) <= (mi - sus[a][0]) * (sus[b][1] - sus[a][1]);
}
int main(){
ll n,k;
cin >> n >> k;
vector<ll> arr(n);
vector<ll> pref(n);
FOR(i,0,n) cin >> arr[i];
pref[0] = arr[0];
FOR(i,1,n) pref[i] = pref[i-1]+arr[i];
FOR(i,0,100001)FOR(j,0,2) dp[i][j] = -1000000000;
dp[0][0] = 0;
FOR(j,1,k+1){
sus.clear();
sus.push_back({-pref[0]+arr[0], dp[0][0], 0});
FOR(i,1,n+1){
ll temp = (pref[n-1] - pref[i-1]);
while (sus.size() >= 2 && (temp * sus[1][0] + sus[1][1]) > (temp * sus[0][0] + sus[0][1])){
sus.pop_front();
}
ll ans = temp * sus[0][0] + sus[0][1] + (pref[n-1] - pref[i-1]) * pref[i-1];
if (dp[i][1] < ans){
dp[i][1] = ans;
prevs[i][j] = sus[0][2];
}
while (sus.size()>=2 && sussy(sus.size()-2, sus.size()-1, (-pref[i]+arr[i]), dp[i][0])) {
sus.pop_back();
}
sus.push_back({(-pref[i]+arr[i]), dp[i][0], i});
}
FOR(i,0,n+1) dp[i][0] = dp[i][1];
FOR(i,0,n+1) dp[i][1] = -1000000000;
}
ll maxi = -1;
ll pos = -1;
FORNEG(i,n-1,-1) if (dp[i][0] > maxi) maxi = dp[i][0], pos = i;
cout << dp[pos][0] << endl;
FOR(i,0,k){
ll sus = prevs[pos][k-i];
cout << pos << " ";
pos = sus;
}
}
# | 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... |