This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In The Name Of God
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <bitset>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sagyndym_seni ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
const ll N = 5e5+5, p1 = 911382323, p2 = 972663749, mod = 1e9+123;
inline int read(){
char c = getchar();
bool minus = 0;
while (c < '0' || '9' < c){
if(c == '-'){ minus = 1;}
c = getchar();
if(c == '-'){ minus = 1;}
}
int res = 0;
while ('0' <= c && c <= '9') {
res = (res << 3) + (res << 1) + c - '0';
c = getchar();
}
if(minus){ res *= -1;}
return res;
}
ll a[N], p[N];
int n;
pair<ll, ll> dp[N];
bool used[N];
ll get(int l, int r){
if(r < l){ return 0;}
return p[r] - (l == 0 ? 0 : p[l - 1]);
}
// {num of blocks, sum in last block}
pair<ll, ll> rec(int r){
if(r == -1){ return {0, 0};}
if(used[r]){ return dp[r];}
used[r] = 1;
for(int i = r - 1; i >= -1; i--){
pair<ll, ll> res = rec(i);
ll sum = get(i + 1, r);
if(res.s <= sum){
if(dp[r].f < res.f + 1){
dp[r] = {res.f + 1, sum};
}else
if(dp[r].f == res.f + 1 && dp[r].s > sum){
dp[r].s = dp[r].s;
}
}
}
return dp[r];
}
int main(){
n = read();
for(int i = 0; i < n; i++){
a[i] = read();
p[i] = a[i] + (i == 0 ? 0 : p[i - 1]);
}
pair<ll, ll> ans = rec(n - 1);
cout<<ans.f;
return 0;
}
/* TIMUS: 292220YC*/
# | 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... |