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 <algorithm>
#include <iostream>
#include <iomanip>
#include <bitset>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
struct segment{int l, r, id;
int size(){return r-l+1;}};
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"
const int maxn = 1e5 + 100;
const ll INF = (1ll<<61);
const int MOD = 1e9 + 7;
const int inf = (1<<30);
const int maxl = 20;
const int P = 31;
int n, k;
ll dp[maxn][202];
ll a[maxn];
void test(){
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
a[i] += a[i-1];
}
fill(dp[0], dp[0] + k + 2, -INF);
dp[0][0] = 0;
for(int i = 1; i <= n; i++){
fill(dp[i], dp[i] + k + 2, -INF);
for(int c = 1; c <= k + 1; c++){
int cnt = 200;
for(int j = 1; j <= i && cnt--; j++){
dp[i][c] = max(dp[j-1][c-1] + (a[i] - a[j-1]) * a[j-1], dp[i][c]);
}
}
}
cout << dp[n][k + 1] << ent;
vector<int> v;
int l = n, c = k + 1;
for(int i = n - 1; i > 0; i--){
if(dp[i][c - 1] + a[i] * (a[l] - a[i]) == dp[l][c]) l = i, c--, v.push_back(i);
}
reverse(v.begin(), v.end());
for(int x: v) cout << x << ' ';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t; t = 1;
while(t--) test();
}
# | 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... |