이 제출은 이전 버전의 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 = 1e5 + 1, inf = 1e15 + 1;
int dp[2][N], par[202][N];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k; cin >> n >> k;
vector <int> p(n), pref(n + 1);
for ( auto &i: p ) cin >> i;
for ( int i = 0; i < n; i++ ){
pref[i + 1] = pref[i] + p[i];
}
k++;
auto cost = [&](int l, int r){
return (pref.back() - pref[r]) * (pref[r] - pref[l - 1]);
};
for ( int i = 0; i < k; i++ ){
swap(dp[1], dp[0]);
memset(dp[1], 0, sizeof(dp[1]));
for ( int j = 1; j <= n; j++ ){
for ( int q = 0; q < j; q++ ){
if ( chmax(dp[1][j], dp[0][q] + cost(q + 1, j)) ){
par[i + 1][j] = q;
}
}
}
}
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... |