이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
#define ln '\n'
#define int long long
template <class _T>
bool chmin(_T &x, const _T &y){
bool flag = false;
if ( x > y ){
x = y; flag |= true;
}
return flag;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool flag = false;
if ( x < y ){
x = y; flag |= true;
}
return flag;
}
const int N = 1e4 + 1, inf = 1e15 + 1;
int dp[2][N], par[202][N], K;
vector <int> pref;
int cost(int l, int r){
return (pref[r] - pref[l - 1]) * (pref.back() - pref[r]);
}
void DAC(int x, int y){
if ( x > y ) return;
int md = (x + y) >> 1;
pair <int,int> Mx = {0, 0};
for ( int i = 0; i + 1 <= md; i++ ){
chmax(Mx, pair <int,int> {dp[0][i] + cost(i + 1, md), i});
}
par[K][md] = Mx.second;
dp[1][md] = Mx.first;
DAC(x, md - 1), DAC(md + 1, y);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k; cin >> n >> k;
vector <int> p(n);
for ( auto &i: p ) cin >> i;
pref.resize(n + 1);
for ( int i = 0; i < n; i++ ){
pref[i + 1] = pref[i] + p[i];
}
k++;
memset(dp[1], 0, sizeof(dp[1]));
for ( int i = 0; i < k; i++ ){
swap(dp[1], dp[0]);
K = i + 1;
DAC(1, n);
}
cout << dp[1][n] << ln;
vector <int> res;
int it = n;
while ( k > 0 ){
it = par[k--][it];
if ( it ) res.pb(it);
}
reverse(all(res));
for ( auto i: res ){
cout << i << ' ';
}
cout << '\n';
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |