제출 #905282

#제출 시각아이디문제언어결과실행 시간메모리
905282andro수열 (APIO14_sequence)C++14
0 / 100
2049 ms16988 KiB
#include <bits/stdc++.h> using namespace std; int main(){ int n, m; cin >> n >> m; vector<int> a(n + 1); for(int i = 1; i <= n; i++) { cin >> a[i]; } vector<int> pref(n + 1); vector<int> suf(n + 2); for(int i = 1; i <= n; i++) { pref[i] = pref[i - 1] + a[i]; } for(int i = n; i > 0; i--) { suf[i] = suf[i + 1] + a[i]; } vector<vector<int>> dp(n + 1, vector<int>(m + 1)); for(int i = 0; i <= n; i++) { for(int j = 0; j <= m; j++) { dp[i][j] = 0; } } vector<vector<int>> P(n + 1, vector<int>(m + 1)); for(int i = 1; i < n; i++) { dp[i][1] = (pref[i]) * (suf[i + 1]); P[i][1] = i; } for(int i = 1; i <= n; i++) { for(int j = 2; j <= min(m, i); j++) { for(int k = 1; k < i; k++) { //dp[i][j] = max(dp[i][j], dp[k][j - 1] + (pref[i] - pref[k]) * suf[i + 1]); int s = dp[k][j - 1] + (pref[i] - pref[k]) * suf[i + 1]; if(s > dp[i][j]) { P[i][j] = k; dp[i][j] = s; } } } } int ans = 0; for(int i = m; i <= n; i++) { ans = max(ans, dp[i][m]); } int x = 0; for(int i = m; i <= n; i++) { if(ans == dp[i][m]) { x = i; } } vector<int> R; R.push_back(x); while(m > 1) { int u = P[x][m]; R.push_back(u); x = u; m--; } reverse(R.begin(), R.end()); cout << ans << "\n"; for(auto it : R) { cout << it << " "; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...