제출 #134909

#제출 시각아이디문제언어결과실행 시간메모리
134909fredbr수열 (APIO14_sequence)C++17
28 / 100
432 ms3832 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int const maxn = 1010; int const maxk = 210; ll const inf = 0x3f3f3f3f3f3f3f3f; ll p[maxn]; ll dp[maxk][maxn], opt[maxk][maxn]; ll solve(int k, int n) { if (dp[k][n] >= 0) return dp[k][n]; if (k == 0) return 0; if (n == 1) return -inf; ll& d = dp[k][n]; ll& op = opt[k][n]; d = 0, op = 0;; for (int i = 1; i < n; i++) { ll cost = p[i] * (p[n]-p[i]) + solve(k-1, i); if (cost > d) { op = i; d = cost; } } return d; } vector<ll> recover(int k, int n) { vector<ll> res; while (k != 0) { res.push_back(opt[k][n]); n = opt[k][n]; k--; } reverse(res.begin(), res.end()); return res; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); memset(dp, -1, sizeof(dp)); int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> p[i]; p[i] += p[i-1]; } ll ans = solve(k, n); auto res = recover(k, n); cout << ans << "\n"; for (ll i : res) cout << i << " "; cout << "\n"; }
#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...