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>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define S second
#define F first
#define pb push_back
using namespace std;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
long long n, k, a[N], pre[N];
void solve(){
cin >> n >> k;
pair<long long, int> dp[n + 1][k + 1];
for(int i = n; i >= 1; --i){
cin >> a[i];
for(int j = 1; j <= k; ++j) dp[i][j].F = INT_MIN;
dp[i][0].F = 0;
}
for(int i = 1; i <= n; ++i) {
pre[i] = pre[i - 1] + a[i];
//cout << a[i] << endl;
}
for(int i = 1; i <= n; ++i){
for(int bars = 1; bars <= k; ++bars){
if(i - 1 < bars) break;
long long sum = a[i];
for(int j = i - 1; j >= 1; --j){
if(dp[j][bars - 1].F + (sum * pre[j]) > dp[i][bars].F){
dp[i][bars].F = dp[j][bars - 1].F + (sum * pre[j]);
dp[i][bars].S = j;
}
sum += a[j];
}
if(dp[i - 1][bars].F > dp[i][bars].F){
dp[i][bars].F = dp[i - 1][bars].F;
dp[i][bars].S = dp[i - 1][bars].S;
}
}
}
cout << dp[n][k].F << '\n';
int pos = n, ct = k;
while(ct > 0){
cout << n - dp[pos][ct].S << ' ';
pos = dp[pos][ct].S;
--ct;
}
}
int main(){
fast;
int t = 1;
// cin >> t;
while(t--)
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... |