# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
760925 | 2023-06-18T22:38:10 Z | usuyus | Cigle (COI21_cigle) | C++14 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> bricks(n); for (int &x : bricks) cin >> x; vector dp(n+1, vector(n+1, 0)); for (int i = 0; i <= n; i++) { for (int j = 1; j < i; j++) { dp[j][i] = max(dp[j][i], dp[j-1][i]); } int balance = 0; int matches = 0; int current = 0; for (int k = i+1, j = i; k <= n; k++) { balance += bricks[k-1]; while (j > 0 && balance - bricks[j-1] >= 0) { balance -= bricks[--j]; } dp[i][k] = max(dp[i][k-1], current); if (balance == 0) { matches++; if (j > 0) current = max(current, dp[j-1][i] + matches); } } } cout << dp[n-1][n] << endl; }