#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=3e3+50;
const int mod=1e9+7;
int n,i,nr,j,t,mx[nmax];
ll a[nmax],f[nmax][nmax],pr[nmax];
vector<pair<ll,int> >vc[nmax];
vector<pair<ll,int> >::iterator it;
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
pr[i]=pr[i-1]+a[i];
}
for(i=0;i<=n;i++)for(j=0;j<=n;j++)f[i][j]=llinf;
f[0][0]=0;
vc[0].pb(mkp(0,0));
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
it=(upper_bound(vc[i-1].begin(),vc[i-1].end(),mkp(pr[j],inf)));
if(it!=vc[i-1].begin())
{
it--;
t=it-vc[i-1].begin();
f[i][j]=pr[j]-pr[mx[t]];
}
/*for(t=j-1;t>=0;t--)
{
if(f[i-1][t]<=pr[j]-pr[t])
{
f[i][j]=pr[j]-pr[t];
break;
}
}*/
if(f[i][j]<llinf)vc[i].pb(mkp(f[i][j]+pr[j],j));
}
if(!vc[i].empty())
{
sort(vc[i].begin(),vc[i].end());
mx[0]=vc[i][0].sc;
}
for(j=1;j<(int)vc[i].size();j++)mx[j]=max(mx[j-1],vc[i][j].sc);
if(f[i][n]<llinf)nr=max(nr,i);
}
cout<<nr<<endl;
return 0;
}
Compilation message
segments.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("O3")
segments.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("unroll-loops")
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
496 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
448 KB |
Output is correct |
11 |
Correct |
2 ms |
456 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
496 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
448 KB |
Output is correct |
11 |
Correct |
2 ms |
456 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
4472 KB |
Output is correct |
17 |
Correct |
6 ms |
4440 KB |
Output is correct |
18 |
Correct |
8 ms |
4856 KB |
Output is correct |
19 |
Correct |
14 ms |
6648 KB |
Output is correct |
20 |
Correct |
17 ms |
7080 KB |
Output is correct |
21 |
Correct |
7 ms |
4672 KB |
Output is correct |
22 |
Correct |
5 ms |
3548 KB |
Output is correct |
23 |
Correct |
4 ms |
2536 KB |
Output is correct |
24 |
Correct |
7 ms |
4728 KB |
Output is correct |
25 |
Correct |
7 ms |
4728 KB |
Output is correct |
26 |
Correct |
19 ms |
7088 KB |
Output is correct |
27 |
Correct |
10 ms |
5596 KB |
Output is correct |
28 |
Correct |
8 ms |
4984 KB |
Output is correct |
29 |
Correct |
16 ms |
7076 KB |
Output is correct |
30 |
Correct |
17 ms |
6776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
496 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
448 KB |
Output is correct |
11 |
Correct |
2 ms |
456 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
4472 KB |
Output is correct |
17 |
Correct |
6 ms |
4440 KB |
Output is correct |
18 |
Correct |
8 ms |
4856 KB |
Output is correct |
19 |
Correct |
14 ms |
6648 KB |
Output is correct |
20 |
Correct |
17 ms |
7080 KB |
Output is correct |
21 |
Correct |
7 ms |
4672 KB |
Output is correct |
22 |
Correct |
5 ms |
3548 KB |
Output is correct |
23 |
Correct |
4 ms |
2536 KB |
Output is correct |
24 |
Correct |
7 ms |
4728 KB |
Output is correct |
25 |
Correct |
7 ms |
4728 KB |
Output is correct |
26 |
Correct |
19 ms |
7088 KB |
Output is correct |
27 |
Correct |
10 ms |
5596 KB |
Output is correct |
28 |
Correct |
8 ms |
4984 KB |
Output is correct |
29 |
Correct |
16 ms |
7076 KB |
Output is correct |
30 |
Correct |
17 ms |
6776 KB |
Output is correct |
31 |
Correct |
74 ms |
72288 KB |
Output is correct |
32 |
Correct |
76 ms |
72468 KB |
Output is correct |
33 |
Correct |
107 ms |
77432 KB |
Output is correct |
34 |
Correct |
521 ms |
134260 KB |
Output is correct |
35 |
Correct |
531 ms |
153860 KB |
Output is correct |
36 |
Correct |
99 ms |
75896 KB |
Output is correct |
37 |
Correct |
81 ms |
62184 KB |
Output is correct |
38 |
Correct |
53 ms |
41976 KB |
Output is correct |
39 |
Correct |
98 ms |
75836 KB |
Output is correct |
40 |
Correct |
98 ms |
76024 KB |
Output is correct |
41 |
Correct |
536 ms |
153876 KB |
Output is correct |
42 |
Correct |
401 ms |
137308 KB |
Output is correct |
43 |
Correct |
176 ms |
88932 KB |
Output is correct |
44 |
Correct |
525 ms |
153916 KB |
Output is correct |
45 |
Correct |
505 ms |
150900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
496 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
448 KB |
Output is correct |
11 |
Correct |
2 ms |
456 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
4472 KB |
Output is correct |
17 |
Correct |
6 ms |
4440 KB |
Output is correct |
18 |
Correct |
8 ms |
4856 KB |
Output is correct |
19 |
Correct |
14 ms |
6648 KB |
Output is correct |
20 |
Correct |
17 ms |
7080 KB |
Output is correct |
21 |
Correct |
7 ms |
4672 KB |
Output is correct |
22 |
Correct |
5 ms |
3548 KB |
Output is correct |
23 |
Correct |
4 ms |
2536 KB |
Output is correct |
24 |
Correct |
7 ms |
4728 KB |
Output is correct |
25 |
Correct |
7 ms |
4728 KB |
Output is correct |
26 |
Correct |
19 ms |
7088 KB |
Output is correct |
27 |
Correct |
10 ms |
5596 KB |
Output is correct |
28 |
Correct |
8 ms |
4984 KB |
Output is correct |
29 |
Correct |
16 ms |
7076 KB |
Output is correct |
30 |
Correct |
17 ms |
6776 KB |
Output is correct |
31 |
Correct |
74 ms |
72288 KB |
Output is correct |
32 |
Correct |
76 ms |
72468 KB |
Output is correct |
33 |
Correct |
107 ms |
77432 KB |
Output is correct |
34 |
Correct |
521 ms |
134260 KB |
Output is correct |
35 |
Correct |
531 ms |
153860 KB |
Output is correct |
36 |
Correct |
99 ms |
75896 KB |
Output is correct |
37 |
Correct |
81 ms |
62184 KB |
Output is correct |
38 |
Correct |
53 ms |
41976 KB |
Output is correct |
39 |
Correct |
98 ms |
75836 KB |
Output is correct |
40 |
Correct |
98 ms |
76024 KB |
Output is correct |
41 |
Correct |
536 ms |
153876 KB |
Output is correct |
42 |
Correct |
401 ms |
137308 KB |
Output is correct |
43 |
Correct |
176 ms |
88932 KB |
Output is correct |
44 |
Correct |
525 ms |
153916 KB |
Output is correct |
45 |
Correct |
505 ms |
150900 KB |
Output is correct |
46 |
Runtime error |
15 ms |
1144 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
47 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
496 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
448 KB |
Output is correct |
11 |
Correct |
2 ms |
456 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
4472 KB |
Output is correct |
17 |
Correct |
6 ms |
4440 KB |
Output is correct |
18 |
Correct |
8 ms |
4856 KB |
Output is correct |
19 |
Correct |
14 ms |
6648 KB |
Output is correct |
20 |
Correct |
17 ms |
7080 KB |
Output is correct |
21 |
Correct |
7 ms |
4672 KB |
Output is correct |
22 |
Correct |
5 ms |
3548 KB |
Output is correct |
23 |
Correct |
4 ms |
2536 KB |
Output is correct |
24 |
Correct |
7 ms |
4728 KB |
Output is correct |
25 |
Correct |
7 ms |
4728 KB |
Output is correct |
26 |
Correct |
19 ms |
7088 KB |
Output is correct |
27 |
Correct |
10 ms |
5596 KB |
Output is correct |
28 |
Correct |
8 ms |
4984 KB |
Output is correct |
29 |
Correct |
16 ms |
7076 KB |
Output is correct |
30 |
Correct |
17 ms |
6776 KB |
Output is correct |
31 |
Correct |
74 ms |
72288 KB |
Output is correct |
32 |
Correct |
76 ms |
72468 KB |
Output is correct |
33 |
Correct |
107 ms |
77432 KB |
Output is correct |
34 |
Correct |
521 ms |
134260 KB |
Output is correct |
35 |
Correct |
531 ms |
153860 KB |
Output is correct |
36 |
Correct |
99 ms |
75896 KB |
Output is correct |
37 |
Correct |
81 ms |
62184 KB |
Output is correct |
38 |
Correct |
53 ms |
41976 KB |
Output is correct |
39 |
Correct |
98 ms |
75836 KB |
Output is correct |
40 |
Correct |
98 ms |
76024 KB |
Output is correct |
41 |
Correct |
536 ms |
153876 KB |
Output is correct |
42 |
Correct |
401 ms |
137308 KB |
Output is correct |
43 |
Correct |
176 ms |
88932 KB |
Output is correct |
44 |
Correct |
525 ms |
153916 KB |
Output is correct |
45 |
Correct |
505 ms |
150900 KB |
Output is correct |
46 |
Runtime error |
15 ms |
1144 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
47 |
Halted |
0 ms |
0 KB |
- |