Submission #1316467

#TimeUsernameProblemLanguageResultExecution timeMemory
1316467AgageldiSplit the sequence (APIO14_sequence)C++20
50 / 100
2093 ms31980 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define N 100005 const int inf = 1e18; int tc = 1, n, a[N], K, sm, p[N], dp[N][201] ,par[N][201]; int P(int x) { return x * x; } int32_t main() { ios::sync_with_stdio(0);cin.tie(0); cin >> n >> K; for(int i = 1; i <= n; i++) { cin >> a[i]; p[i] = a[i] + p[i - 1]; } for(int i = 1; i <= n; i++) { dp[i][0] = p[i]*p[i]; } dp[0][0] = 0; for(int j = 1; j <= K; j++) dp[0][j] = inf; for(int i = 1; i <= n; i++) { for(int j = 1; j <= K; j++) { dp[i][j] = inf; for(int k = i; k > j; k--) { int best = (P(p[i] - p[k - 1]) + dp[k - 1][j - 1]); if(best < dp[i][j]) { par[i][j] = k - 1; dp[i][j] = best; } } } } cout<< (p[n]*p[n] - dp[n][K]) / 2 << '\n'; int p = K; vector <int> ans; int pos = n; while(par[pos][p] > 0) { pos = par[pos][p]; ans.push_back(pos); p--; } reverse(ans.begin(),ans.end()); for(auto i : ans) { cout << i << " "; } cout << '\n'; return 0; }
#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...