#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define endl "\n"
const double PI=3.141592653589;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
int n;
vector<int>a,pref;
struct segtree{
int sz = 1;
vector<int>seg,lazy;
void init(int n){
while(sz < n)sz*=2;
seg = vector<int>(sz*2);
lazy = vector<int>(sz*2);
}
void upd(int x, int lx, int rx, int l, int r, int v){
// range add v
if(lazy[x]){
seg[x]+=lazy[x];
if(rx-lx > 1){
lazy[2*x+1]+=lazy[x];
lazy[2*x+2]+=lazy[x];
}
lazy[x] = 0;
}
if(lx >= l && rx <= r){
seg[x] += v;
if(rx-lx > 1){
lazy[2*x+1]+=v;
lazy[2*x+2]+=v;
}
return;
}
if(rx <= l || lx >= r)return;
int m = (lx+rx)/2;
upd(2*x+1,lx,m,l,r,v);
upd(2*x+2,m,rx,l,r,v);
seg[x] = min(seg[2*x+1],seg[2*x+2]);
}
void upd(int l, int r, int v){
upd(0,0,sz,l,r,v);
}
int sol(int x, int lx, int rx, int l, int r){
if(lazy[x]){
seg[x]+=lazy[x];
if(rx-lx > 1){
lazy[2*x+1]+=lazy[x];
lazy[2*x+2]+=lazy[x];
}
lazy[x] = 0;
}
if(seg[x] > 0 || lx >= r || rx <= l)return -1;
if(rx-lx == 1)return lx;
int m = (lx+rx)/2;
int res = sol(2*x+2,m,rx,l,r);
if(res==-1)res = sol(2*x+1,lx,m,l,r);
return res;
}
int sol(int l, int r){
// find first elmeent <= 0 in range [l,r]
return sol(0,0,sz,l,r);
}
};
segtree st;
int f(int x){
vector<pair<int,int>>dp(n+1);
vector<int>mn(n+5,0);
// pref[i]-pref[j]-dp[j].second >= 0
// pref[i] >= pref[j]+dp[j].second;
int pos = 0;
for(int i = 1;i<=n;++i){
dp[i].second = dp[i-1].second+a[i];
if(dp[i].second >= x){
pos = i;
dp[i].first = 1;
int val = lower_bound(all(pref),pref[i]+dp[i].second)-pref.begin();
if(val <= n)mn[val] = max(mn[val],i);
break;
}
}
if(pos==0)return 0;
// pos denotes starting position of maxdp value so far
vector<pair<int,int>>p;
for(int i = pos+1;i<=n;++i){
dp[i] = {dp[i-1].first,dp[i-1].second+a[i]};
pair<int,int>get = {dp[mn[i]].first+1,pref[i]-pref[mn[i]]};
if(get.first > dp[i].first || (get.first==dp[i].first && get.second < dp[i].second))dp[i] = get;
int val = lower_bound(all(pref),pref[i]+dp[i].second)-pref.begin();
if(val <= n)mn[val] = i;
}
return dp[n].first;
}
void solve()
{
cin >> n;
a.resize(n+1);
pref.resize(n+1);
for(int i = 1;i<=n;++i)cin >> a[i];
for(int i = 1;i<=n;++i)pref[i] = pref[i-1]+a[i];
int l = 1, r = 1e18;
while(l+2 < r){
int m1 = (l+(r-l)/3), m2 = (r-(r-l)/3);
if(f(m1) < f(m2))l = m1;
else r = m2;
}
int res = 0;
for(int i = l;i<=r;++i)res = max(res, f(i));
cout << res << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int T=1;
for(int i = 1;i<=T;++i)
{
// cout << "Case #" << i << ": ";
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
324 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
2 ms |
316 KB |
Output is correct |
30 |
Correct |
2 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
324 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
2 ms |
316 KB |
Output is correct |
30 |
Correct |
2 ms |
316 KB |
Output is correct |
31 |
Correct |
6 ms |
340 KB |
Output is correct |
32 |
Correct |
10 ms |
440 KB |
Output is correct |
33 |
Correct |
15 ms |
396 KB |
Output is correct |
34 |
Correct |
12 ms |
464 KB |
Output is correct |
35 |
Correct |
19 ms |
340 KB |
Output is correct |
36 |
Correct |
20 ms |
384 KB |
Output is correct |
37 |
Correct |
17 ms |
384 KB |
Output is correct |
38 |
Correct |
13 ms |
420 KB |
Output is correct |
39 |
Correct |
9 ms |
340 KB |
Output is correct |
40 |
Correct |
19 ms |
464 KB |
Output is correct |
41 |
Correct |
8 ms |
340 KB |
Output is correct |
42 |
Correct |
9 ms |
340 KB |
Output is correct |
43 |
Correct |
7 ms |
340 KB |
Output is correct |
44 |
Correct |
11 ms |
340 KB |
Output is correct |
45 |
Correct |
11 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
324 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
2 ms |
316 KB |
Output is correct |
30 |
Correct |
2 ms |
316 KB |
Output is correct |
31 |
Correct |
6 ms |
340 KB |
Output is correct |
32 |
Correct |
10 ms |
440 KB |
Output is correct |
33 |
Correct |
15 ms |
396 KB |
Output is correct |
34 |
Correct |
12 ms |
464 KB |
Output is correct |
35 |
Correct |
19 ms |
340 KB |
Output is correct |
36 |
Correct |
20 ms |
384 KB |
Output is correct |
37 |
Correct |
17 ms |
384 KB |
Output is correct |
38 |
Correct |
13 ms |
420 KB |
Output is correct |
39 |
Correct |
9 ms |
340 KB |
Output is correct |
40 |
Correct |
19 ms |
464 KB |
Output is correct |
41 |
Correct |
8 ms |
340 KB |
Output is correct |
42 |
Correct |
9 ms |
340 KB |
Output is correct |
43 |
Correct |
7 ms |
340 KB |
Output is correct |
44 |
Correct |
11 ms |
340 KB |
Output is correct |
45 |
Correct |
11 ms |
340 KB |
Output is correct |
46 |
Correct |
260 ms |
4616 KB |
Output is correct |
47 |
Correct |
452 ms |
4708 KB |
Output is correct |
48 |
Correct |
655 ms |
5064 KB |
Output is correct |
49 |
Correct |
859 ms |
5164 KB |
Output is correct |
50 |
Correct |
810 ms |
5212 KB |
Output is correct |
51 |
Correct |
869 ms |
5220 KB |
Output is correct |
52 |
Correct |
423 ms |
2764 KB |
Output is correct |
53 |
Correct |
268 ms |
1780 KB |
Output is correct |
54 |
Correct |
385 ms |
4452 KB |
Output is correct |
55 |
Correct |
825 ms |
5224 KB |
Output is correct |
56 |
Correct |
516 ms |
4712 KB |
Output is correct |
57 |
Correct |
428 ms |
4652 KB |
Output is correct |
58 |
Correct |
466 ms |
4692 KB |
Output is correct |
59 |
Correct |
591 ms |
4820 KB |
Output is correct |
60 |
Correct |
649 ms |
4820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
324 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
2 ms |
316 KB |
Output is correct |
30 |
Correct |
2 ms |
316 KB |
Output is correct |
31 |
Correct |
6 ms |
340 KB |
Output is correct |
32 |
Correct |
10 ms |
440 KB |
Output is correct |
33 |
Correct |
15 ms |
396 KB |
Output is correct |
34 |
Correct |
12 ms |
464 KB |
Output is correct |
35 |
Correct |
19 ms |
340 KB |
Output is correct |
36 |
Correct |
20 ms |
384 KB |
Output is correct |
37 |
Correct |
17 ms |
384 KB |
Output is correct |
38 |
Correct |
13 ms |
420 KB |
Output is correct |
39 |
Correct |
9 ms |
340 KB |
Output is correct |
40 |
Correct |
19 ms |
464 KB |
Output is correct |
41 |
Correct |
8 ms |
340 KB |
Output is correct |
42 |
Correct |
9 ms |
340 KB |
Output is correct |
43 |
Correct |
7 ms |
340 KB |
Output is correct |
44 |
Correct |
11 ms |
340 KB |
Output is correct |
45 |
Correct |
11 ms |
340 KB |
Output is correct |
46 |
Correct |
260 ms |
4616 KB |
Output is correct |
47 |
Correct |
452 ms |
4708 KB |
Output is correct |
48 |
Correct |
655 ms |
5064 KB |
Output is correct |
49 |
Correct |
859 ms |
5164 KB |
Output is correct |
50 |
Correct |
810 ms |
5212 KB |
Output is correct |
51 |
Correct |
869 ms |
5220 KB |
Output is correct |
52 |
Correct |
423 ms |
2764 KB |
Output is correct |
53 |
Correct |
268 ms |
1780 KB |
Output is correct |
54 |
Correct |
385 ms |
4452 KB |
Output is correct |
55 |
Correct |
825 ms |
5224 KB |
Output is correct |
56 |
Correct |
516 ms |
4712 KB |
Output is correct |
57 |
Correct |
428 ms |
4652 KB |
Output is correct |
58 |
Correct |
466 ms |
4692 KB |
Output is correct |
59 |
Correct |
591 ms |
4820 KB |
Output is correct |
60 |
Correct |
649 ms |
4820 KB |
Output is correct |
61 |
Execution timed out |
1511 ms |
21868 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |