Submission #716787

#TimeUsernameProblemLanguageResultExecution timeMemory
716787pragmatistSplit the sequence (APIO14_sequence)C++17
0 / 100
125 ms6800 KiB
#include <bits/stdc++.h> #define ll long long #define pb push_back #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define sz(v) (int)v.size() #define x first #define y second #define nl "\n" using namespace std; const int N = (int)1e5 + 7; const int M = (int)2e6 + 5e5 + 7; const ll MOD = (ll)1e9; const int inf = (int)1e9 + 7; const int B = (int)390; const ll INF = (ll)1e18 + 7; pair<int, int> dir[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; bool bit(int mask, int i) { return (mask >> i & 1); } int sum(int x, int y) { x += y; if(x >= MOD) x -= MOD; return x; } int mult(int x, int y) { return 1ll * x * y % MOD; } int n, k, a[N], suf[N], p[1001][201]; ll dp[1001][201]; void solve() { cin >> n >> k; for(int i = 1; i <= n; ++i) cin >> a[i]; for(int i = n; i >= 1; --i) suf[i] = suf[i + 1] + a[i]; ll ans = -INF; int lst = -1; for(int i = 1; i < n; ++i) { for(int l = 1; l <= k; ++l) { dp[i][l] = -INF; for(int j = i; j >= 1; --j) { if(dp[j - 1][l - 1] + (suf[j] - suf[i + 1]) * suf[i + 1] > dp[i][l]) { dp[i][l] = dp[j - 1][l - 1] + (suf[j] - suf[i + 1]) * suf[i + 1]; p[i][l] = j; } } } if(dp[i][k] > ans) { ans = dp[i][k]; lst = i; } } assert(lst != -1); vector<int> v; while(lst > 0) { v.pb(lst); lst = p[lst][k--] - 1; } reverse(all(v)); cout << ans << nl; for(auto x : v) cout << x << ' '; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); //freopen("sequence.in", "r", stdin); //freopen("sequence.out", "w", stdout); int test = 1; //cin >> test; for(int i = 1; i <= test; ++i) { //cout << "Case " << i << ":\n"; solve(); } return 0; } /* possible causes of error : * array bounds * int overflow * special case (n == 1?) * haven't clean something * wrong N | M | inf | INF | MOD * try to write brute force to find test * don't waste time * don't write bullshit * solution is easy */
#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...