#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
// #pragma GCC target("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
//-------------------------------------------------------------------------------------------------------------
#define int long long
#define pb push_back
#define ew "\n"
#define all(x) (x).begin(), (x).end()
#define st first
#define nd second
#define len(x) (int)x.size()
#define eb emplace_back
#define turbo \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
const int maxn = 5e5 + 12;
const int mxx = 100100;
const int MOD = 1e9 + 7;
const int inf = 2e18 + 7;
const int HH = 1e2 + 70;
int n;
int a[maxn], pref[maxn], sm[maxn], dp[maxn];
void code()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sm[i] = inf;
pref[i] = pref[i - 1] + a[i];
}
for (int i = 1; i <= n; i++)
{
int lst = dp[i - 1] - 1;
for (int j = i - 1; j >= 0; j--)
{
if (dp[j] < lst)
{
break;
}
if (dp[i] < dp[j] || (dp[i] == dp[j] && sm[j] + pref[i] - pref[j] < sm[i]))
{
dp[i] = dp[j];
sm[i] = sm[j] + pref[i] - pref[j];
}
if (sm[j] <= pref[i] - pref[j] && (dp[i] < dp[j] + 1 || (dp[i] == dp[j] + 1 && pref[i] - pref[j] < sm[i])))
{
dp[i] = dp[j] + 1;
sm[i] = pref[i] - pref[j];
}
}
}
cout << dp[n];
}
int32_t main()
{
// freopen("248.in", "r", stdin);
// freopen("248.out", "w", stdout);
// turbo;
int tt = 1, j = 1;
// cin >> tt;
while (tt--)
{
// cout << "Case " << j << ":" << ew;
code();
// j++;
}
}
// by sl1pzz
// going to respa silver/goldz
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |