이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define endl "\n"
void printVector(vector<int> a){
for (auto x: a) cout << x << " ";
cout << endl;
}
int max(int x, int y){
if (x > y) return x;
else return y;
}
void solve(){
int n, k; cin >> n >> k;
vector<int> a(n+1);
FOR(i,0,n) cin >> a[i+1];
vector<int> prefix(n+1);
FOR(i,1,n+1) prefix[i] = prefix[i-1]+a[i];
vector<vector<int>> dp(n+1, vector<int>(k+2));
vector<vector<int>> prev(n+1, vector<int>(k+2));
for (int j = 1; j <= k+1; ++j){
for (int i = 1; i <= n; ++i){
if (j > i) continue;
for (int u = 0; u < i; u++){
if (dp[u][j-1]+(prefix[i]-prefix[u])*(prefix[n]-prefix[i]) >= dp[i][j]){
prev[i][j] = u;
dp[i][j] = dp[u][j-1]+(prefix[i]-prefix[u])*(prefix[n]-prefix[i]);
}
}
}
}
int u = n;
int h = k+1;
cout << dp[n][k+1] << endl;
while (prev[u][h] != 0 && h > 1){
cout << prev[u][h] << " ";
u = prev[u][h];
h--;
}
cout << endl;
}
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", stdin)
int t = 1; // cin >> t;
while (t--) solve();
}
# | 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... |