#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define threesum cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false)
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define int long long
#define double long double
#define pii pair<int, int>
#define ppp pair<int, pii>
#define dout cout << fixed << setprecision(15)
#define mid ((l + r) / 2)
#define lc (2 * id)
#define rc (lc + 1)
const int maxn = 3e5 + 10, maxm = 1e3 + 10, oo = 1e18 + 10, lg = 8, sq = 350, mod = 1e9 + 7;
int n, a[maxn], ps[maxn], dp[2][maxn], ans;
signed main()
{
threesum;
cin >> n;
for (int i = 1; i <= n;i++)
cin >> a[i];
for (int i = 1; i <= n;i++)
ps[i] = ps[i - 1] + a[i];
for (int i = 1; i <= n; i++){
dp[0][i] = dp[0][i - 1];
dp[1][i] = dp[1][i - 1] + a[i];
for (int j = i - 1; j >= 0; j--)
{
if(ps[i] - ps[j] >= dp[1][j] && (dp[0][j] + 1 > dp[0][i] || (dp[0][j] + 1 >= dp[0][i] && ps[i] - ps[j] < dp[1][i]))){
dp[0][i] = dp[0][j] + 1;
dp[1][i] = ps[i] - ps[j];
}
}
//cout << i << " " << dp[0][i] << " " << dp[1][i] << "\n";
}
cout << dp[0][n];
}
# | 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... |