#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
const int mod = 1e9+7, N = 5005;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val);}
int a[N], n, m, k;
vector<pii> dp[N];
int pr[N];
void solve(int test_case){
int i, j;
cin >> n;
for(i=1;i<=n;i++){
cin >> a[i];
pr[i]=a[i];pr[i]+=pr[i-1];
}
for(i=1;i<=n;i++){
dp[i].pb({pr[i], 1});
}
for(i=1;i<=n;i++){
for(j=1;j<i;j++){
int s = pr[i]-pr[j];
int pos = lower_bound(all(dp[j]), pii{s,-1})-dp[j].begin();pos--;
int mx=0;
bool f = false;//if(pos<0)f=false;
//if(i == 5)cout << s << '\n';
for(k=0;k<dp[j].size();k++){
if(dp[j][k].ff > s)continue;
mx = max(mx, dp[j][k].ss);f=true;
//if(i==5)cout << dp[j][k].ff << ' ' << dp[j][k].ss << '\n';
}
if(f)dp[i].pb({s, mx+1});
}
}
/* for(i=1;i<=n;i++){
cout << "doing " << i<< '\n';
for(auto [x, y] : dp[i]){
cout << x << ' ' << y << '\n';
}
}
*/
cout << dp[n].back().ss << '\n';
return;
}
signed main(){
FASTIO;
#define MULTITEST 0
#if MULTITEST
int ___T;
cin >> ___T;
for(int T_CASE = 1; T_CASE <= ___T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
return 0;
}
Compilation message
segments.cpp: In function 'void solve(long long int)':
segments.cpp:34:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(k=0;k<dp[j].size();k++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
25 ms |
2668 KB |
Output is correct |
18 |
Correct |
29 ms |
3052 KB |
Output is correct |
19 |
Correct |
29 ms |
3180 KB |
Output is correct |
20 |
Correct |
30 ms |
3180 KB |
Output is correct |
21 |
Correct |
29 ms |
3052 KB |
Output is correct |
22 |
Correct |
16 ms |
2156 KB |
Output is correct |
23 |
Correct |
7 ms |
1388 KB |
Output is correct |
24 |
Correct |
31 ms |
3180 KB |
Output is correct |
25 |
Correct |
29 ms |
3052 KB |
Output is correct |
26 |
Correct |
31 ms |
3180 KB |
Output is correct |
27 |
Correct |
23 ms |
2284 KB |
Output is correct |
28 |
Correct |
31 ms |
3052 KB |
Output is correct |
29 |
Correct |
31 ms |
3180 KB |
Output is correct |
30 |
Correct |
43 ms |
3180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
25 ms |
2668 KB |
Output is correct |
18 |
Correct |
29 ms |
3052 KB |
Output is correct |
19 |
Correct |
29 ms |
3180 KB |
Output is correct |
20 |
Correct |
30 ms |
3180 KB |
Output is correct |
21 |
Correct |
29 ms |
3052 KB |
Output is correct |
22 |
Correct |
16 ms |
2156 KB |
Output is correct |
23 |
Correct |
7 ms |
1388 KB |
Output is correct |
24 |
Correct |
31 ms |
3180 KB |
Output is correct |
25 |
Correct |
29 ms |
3052 KB |
Output is correct |
26 |
Correct |
31 ms |
3180 KB |
Output is correct |
27 |
Correct |
23 ms |
2284 KB |
Output is correct |
28 |
Correct |
31 ms |
3052 KB |
Output is correct |
29 |
Correct |
31 ms |
3180 KB |
Output is correct |
30 |
Correct |
43 ms |
3180 KB |
Output is correct |
31 |
Correct |
30 ms |
620 KB |
Output is correct |
32 |
Execution timed out |
1593 ms |
25248 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
25 ms |
2668 KB |
Output is correct |
18 |
Correct |
29 ms |
3052 KB |
Output is correct |
19 |
Correct |
29 ms |
3180 KB |
Output is correct |
20 |
Correct |
30 ms |
3180 KB |
Output is correct |
21 |
Correct |
29 ms |
3052 KB |
Output is correct |
22 |
Correct |
16 ms |
2156 KB |
Output is correct |
23 |
Correct |
7 ms |
1388 KB |
Output is correct |
24 |
Correct |
31 ms |
3180 KB |
Output is correct |
25 |
Correct |
29 ms |
3052 KB |
Output is correct |
26 |
Correct |
31 ms |
3180 KB |
Output is correct |
27 |
Correct |
23 ms |
2284 KB |
Output is correct |
28 |
Correct |
31 ms |
3052 KB |
Output is correct |
29 |
Correct |
31 ms |
3180 KB |
Output is correct |
30 |
Correct |
43 ms |
3180 KB |
Output is correct |
31 |
Correct |
30 ms |
620 KB |
Output is correct |
32 |
Execution timed out |
1593 ms |
25248 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
25 ms |
2668 KB |
Output is correct |
18 |
Correct |
29 ms |
3052 KB |
Output is correct |
19 |
Correct |
29 ms |
3180 KB |
Output is correct |
20 |
Correct |
30 ms |
3180 KB |
Output is correct |
21 |
Correct |
29 ms |
3052 KB |
Output is correct |
22 |
Correct |
16 ms |
2156 KB |
Output is correct |
23 |
Correct |
7 ms |
1388 KB |
Output is correct |
24 |
Correct |
31 ms |
3180 KB |
Output is correct |
25 |
Correct |
29 ms |
3052 KB |
Output is correct |
26 |
Correct |
31 ms |
3180 KB |
Output is correct |
27 |
Correct |
23 ms |
2284 KB |
Output is correct |
28 |
Correct |
31 ms |
3052 KB |
Output is correct |
29 |
Correct |
31 ms |
3180 KB |
Output is correct |
30 |
Correct |
43 ms |
3180 KB |
Output is correct |
31 |
Correct |
30 ms |
620 KB |
Output is correct |
32 |
Execution timed out |
1593 ms |
25248 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |