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;
inline long long msb(long long val){return 63-__builtin_clzll(val);}
inline int msb(int val){return 31-__builtin_clz(val);}
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
template<class T> bool chmin(T& a, const T& b) {return a>b? a=b, true:false;}
template<class T> bool chmax(T& a, const T& b) {return a<b? a=b, true:false;}
const int mod = 1e9+7, N = 1e5+5;
int a[N], n, m, k;
vector<int> old_dp, new_dp;
int32_t par[201][N];
inline int cost(int l, int r){
return (a[r] - a[l-1]) * (a[n]-a[r]);
}
int K;
void divide_and_conquer(int l, int r, int optl, int optr){
if(l > r)return;
pii opt = {-1, -1};
int mid = (l+r)/2;
for(int i=optl;i<=min(mid-1, optr);i++){
chmax(opt, pii{old_dp[i] + cost(i+1,mid),i});
}
par[K][mid] = opt.second;
new_dp[mid] = opt.first;
divide_and_conquer(l,mid-1,optl,opt.second);
divide_and_conquer(mid+1,r,opt.second,optr);
}
void solve(int test_case){
int i, j;
cin >> n >> k;
old_dp.assign(n+1,0);
new_dp.assign(n+1,0);
for(i=1;i<=n;i++)cin >> a[i], a[i] += a[i-1];
for(i=1;i<=n;i++){
new_dp[i] = cost(1,i);
par[1][i] = 0;
}
for(j=2;j<=k;j++){
K = j;
swap(old_dp, new_dp);
divide_and_conquer(j,n,1,n);
}
int ans = -1, idx = -1;
for(i=1;i<=n;i++)if(chmax(ans, new_dp[i]))idx = i;
cout << ans << '\n';
vector<int> res;
for(j=k;j>=1;j--){
res.pb(idx);
idx = par[j][idx];
}reverse(all(res));
for(auto x : res)cout << x << ' ';
cout << '\n';
return;
}
signed main(){
FASTIO;
//~ #define MULTITEST 1
#if MULTITEST
int _T;
cin >> _T;
for(int T_CASE = 1; T_CASE <= _T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
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... |