//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
void solve() {
ll n;
cin >> n;
vector<ll> arr(n);
for (ll i = 0; i < n; i++) {
cin >> arr[i];
}
vector<vector<ll>> dp(n, vector<ll>(n, 0));
for (ll i = 0; i < n - 1; i++) {
vector<ll> all(n, 0), col(n, 0);
ll now = i + 1;
ll ns1 = 0, ns2 = 0;
ll na = 0;
for (ll j = i; j >= 0; j--) {
ns1 += arr[j];
while (now < n && ns2 < ns1) {
col[now] = na;
ns2 += arr[now];
now++;
}
col[j] = na;
if (ns2 == ns1) {
na++;
}
all[j] = now - 1;
}
for (ll j = now; j < n; j++) {
col[j] = na;
}
deque<pair<ll, ll>> q;
for (ll j = i; j >= 0; j--) {
assign_max(dp[i + 1][j], dp[i][j]);
if (all[i] + 1 < n) {
assign_max(dp[all[i] + 1][i + 1], dp[i][j] + col[j]);
}
while (!q.empty() && q.back().second < dp[i][j]) {
q.pop_back();
}
q.emplace_back(all[i], dp[i][j]);
}
for (ll j = i + 1; j < n; j++) {
while (!q.empty() && q.front().first < j) {
q.pop_front();
}
if (q.empty()) {
break;
}
assign_max(dp[j][i + 1], q.front().second + col[j]);
}
}
ll ans = 0;
for (ll i = 0; i < n; i++) {
assign_max(ans, dp.back()[i]);
}
cout << ans << '\n';
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message
cigle.cpp:12:22: warning: overflow in conversion from 'long int' to 'll' {aka 'int'} changes value from '9000000000000000000' to '-494665728' [-Woverflow]
12 | constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
cigle.cpp: In function 'int main()':
cigle.cpp:109:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
cigle.cpp:110:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |