//#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;
}
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[j] + 1 < n) {
assign_max(dp[all[j] + 1][i + 1], dp[i][j] + col[j]);
}
while (!q.empty() && q.back().second < dp[i][j]) {
q.pop_back();
}
q.emplace_back(all[j], 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 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1372 KB |
Output is correct |
2 |
Correct |
2 ms |
1372 KB |
Output is correct |
3 |
Correct |
4 ms |
1372 KB |
Output is correct |
4 |
Correct |
2 ms |
1368 KB |
Output is correct |
5 |
Correct |
2 ms |
1368 KB |
Output is correct |
6 |
Correct |
2 ms |
1464 KB |
Output is correct |
7 |
Correct |
3 ms |
1464 KB |
Output is correct |
8 |
Correct |
3 ms |
1368 KB |
Output is correct |
9 |
Correct |
3 ms |
1368 KB |
Output is correct |
10 |
Correct |
2 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1464 KB |
Output is correct |
12 |
Correct |
2 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
4 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1368 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1464 KB |
Output is correct |
27 |
Correct |
3 ms |
1464 KB |
Output is correct |
28 |
Correct |
3 ms |
1368 KB |
Output is correct |
29 |
Correct |
3 ms |
1368 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1464 KB |
Output is correct |
32 |
Correct |
2 ms |
1368 KB |
Output is correct |
33 |
Correct |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
2 ms |
1304 KB |
Output is correct |
35 |
Correct |
2 ms |
1372 KB |
Output is correct |
36 |
Correct |
2 ms |
1372 KB |
Output is correct |
37 |
Correct |
2 ms |
1372 KB |
Output is correct |
38 |
Correct |
2 ms |
1372 KB |
Output is correct |
39 |
Correct |
3 ms |
1624 KB |
Output is correct |
40 |
Correct |
2 ms |
1372 KB |
Output is correct |
41 |
Correct |
2 ms |
1372 KB |
Output is correct |
42 |
Correct |
3 ms |
1372 KB |
Output is correct |
43 |
Correct |
2 ms |
1372 KB |
Output is correct |
44 |
Correct |
2 ms |
1372 KB |
Output is correct |
45 |
Correct |
2 ms |
1372 KB |
Output is correct |
46 |
Correct |
2 ms |
1372 KB |
Output is correct |
47 |
Correct |
2 ms |
1624 KB |
Output is correct |
48 |
Correct |
2 ms |
1368 KB |
Output is correct |
49 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
4 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1368 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1464 KB |
Output is correct |
27 |
Correct |
3 ms |
1464 KB |
Output is correct |
28 |
Correct |
3 ms |
1368 KB |
Output is correct |
29 |
Correct |
3 ms |
1368 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1464 KB |
Output is correct |
32 |
Correct |
2 ms |
1368 KB |
Output is correct |
33 |
Correct |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
2 ms |
1304 KB |
Output is correct |
35 |
Correct |
2 ms |
1372 KB |
Output is correct |
36 |
Correct |
2 ms |
1372 KB |
Output is correct |
37 |
Correct |
2 ms |
1372 KB |
Output is correct |
38 |
Correct |
2 ms |
1372 KB |
Output is correct |
39 |
Correct |
3 ms |
1624 KB |
Output is correct |
40 |
Correct |
2 ms |
1372 KB |
Output is correct |
41 |
Correct |
2 ms |
1372 KB |
Output is correct |
42 |
Correct |
3 ms |
1372 KB |
Output is correct |
43 |
Correct |
2 ms |
1372 KB |
Output is correct |
44 |
Correct |
2 ms |
1372 KB |
Output is correct |
45 |
Correct |
2 ms |
1372 KB |
Output is correct |
46 |
Correct |
2 ms |
1372 KB |
Output is correct |
47 |
Correct |
2 ms |
1624 KB |
Output is correct |
48 |
Correct |
2 ms |
1368 KB |
Output is correct |
49 |
Correct |
2 ms |
1372 KB |
Output is correct |
50 |
Correct |
167 ms |
98560 KB |
Output is correct |
51 |
Correct |
167 ms |
98384 KB |
Output is correct |
52 |
Correct |
184 ms |
98540 KB |
Output is correct |
53 |
Correct |
186 ms |
98468 KB |
Output is correct |
54 |
Correct |
175 ms |
98548 KB |
Output is correct |
55 |
Correct |
173 ms |
98384 KB |
Output is correct |
56 |
Correct |
249 ms |
98556 KB |
Output is correct |
57 |
Correct |
250 ms |
98532 KB |
Output is correct |
58 |
Correct |
276 ms |
98384 KB |
Output is correct |
59 |
Correct |
266 ms |
98384 KB |
Output is correct |
60 |
Correct |
258 ms |
98556 KB |
Output is correct |
61 |
Correct |
247 ms |
98504 KB |
Output is correct |
62 |
Correct |
224 ms |
98568 KB |
Output is correct |
63 |
Correct |
241 ms |
98552 KB |
Output is correct |
64 |
Correct |
224 ms |
98552 KB |
Output is correct |
65 |
Correct |
200 ms |
98384 KB |
Output is correct |
66 |
Correct |
208 ms |
98384 KB |
Output is correct |
67 |
Correct |
182 ms |
98388 KB |
Output is correct |
68 |
Correct |
195 ms |
98380 KB |
Output is correct |
69 |
Correct |
191 ms |
98548 KB |
Output is correct |
70 |
Correct |
263 ms |
98548 KB |
Output is correct |
71 |
Correct |
258 ms |
98492 KB |
Output is correct |
72 |
Correct |
238 ms |
98548 KB |
Output is correct |