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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> v(100005);
vector<vector<ll>> dp(2,vector<ll>(100005));
vector<vector<int>> L(100005,vector<int>(205));
int k;
void find(int l, int r, int lb, int rb, int Z){
if(l > r)return;
int mid = (l+r)>>1;
int pos = lb; ll ma = -1;
for(int i = lb; i < min(mid,rb+1); ++i){
if(ma < 1ll * dp[0][i]+ 1ll * (v[mid]-v[i])*v[i]){
ma = 1ll * dp[0][i]+ 1ll * (v[mid]-v[i])*v[i];
pos = i;
}
}
dp[1][mid] = ma;
L[mid][Z] = pos;
find(l,mid-1,lb,pos,Z);
find(mid+1,r,pos,rb,Z);
}
int main(){
int n; cin >> n >> k;
for(int i = 0; i++ < n;){
cin >> v[i];
}
if(n == 2){
cout << v[1]*v[2] << "\n";
cout << 1 << "\n";
}
for(int i = 0; i++ < n;){
v[i]+=v[i-1];
}
for(int i = 1; i<=k; ++i){
find(1,n,1,n,i);
for(int j = 1; j <= n; ++j)dp[0][j] = dp[1][j];
}
ll ans = 0; int ind = 0;
for(int i = 1; i <= n; ++i){
if(ans < dp[0][i]){
ind = i;
ans = dp[0][i];
}
}
cout << ans << "\n";
while(k--){
cout << L[ind][k]+1 << " ";
ind = L[ind][k];
}
cout << "\n";
}
# | 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... |