#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = (int)53;
const ll LINF = (ll)1e18;
int n, A, B;
ll a[maxn], pre[maxn]; ll ans=LINF;
bool dp[maxn][maxn][25][1028];
void recur(int pos, int lpos, int knum, int Or){
if(dp[pos][lpos][knum][Or]) return;
dp[pos][lpos][knum][Or]=1;
if(pos==n+1) return;
if(pos!=n) recur(pos+1, lpos, knum, Or);
recur(pos+1, pos, knum+1, Or|(pre[pos]-pre[lpos]));
}
int32_t main() {
cin >> n >> A >> B;
for(int i = 0; i < n; i++) cin >> a[i], pre[i+1]=pre[i]+a[i];
if(n<=20){
for(int mask = 0; mask < (1<<n); mask++){
int num = __builtin_popcount(mask);
if(num<A or num>B or !(mask&(1<<(n-1)))) continue;
ll Or = 0, sum = 0;
for(int i = 0; i < n; i++){
if(mask&(1<<i)) sum+=a[i], Or|=sum, sum=0;
else sum+=a[i];
}
ans = min(ans, Or);
}
}
else if(n<=50){
recur(1,0,1,0);
for(int i = A+1; i <= B+1; i++)
for(int k = 0; k < 1028; k++)
if(dp[n+1][n][B+1][k]) ans = min(ans, (ll)k);
}
else{
const int mxOR = 1000;
ll dp[n+1][mxOR];
for(int i = 0; i <= n; i++)
for(int j = 0; j < mxOR; j++)
dp[i][j]=LINF;
dp[0][0]=0; ll x;
for(int i = 1; i <= n; i++)
for(int j = 0; j < i; j++)
for(int k = 0; k < mxOR; k++)
x=pre[i]-pre[j], dp[i][j|x]=min(dp[i][j|x], dp[k][j]+1);
for(int j = 0; j < mxOR; j++)
if(dp[n][j]<=B) ans=min(ans,(ll)j);
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
4 ms |
304 KB |
Output is correct |
9 |
Correct |
5 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
308 KB |
Output is correct |
13 |
Correct |
29 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
6 ms |
212 KB |
Output is correct |
22 |
Correct |
6 ms |
300 KB |
Output is correct |
23 |
Correct |
7 ms |
212 KB |
Output is correct |
24 |
Correct |
4 ms |
212 KB |
Output is correct |
25 |
Correct |
6 ms |
300 KB |
Output is correct |
26 |
Correct |
28 ms |
284 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
2 ms |
212 KB |
Output is correct |
31 |
Correct |
7 ms |
212 KB |
Output is correct |
32 |
Correct |
4 ms |
212 KB |
Output is correct |
33 |
Correct |
28 ms |
308 KB |
Output is correct |
34 |
Correct |
35 ms |
284 KB |
Output is correct |
35 |
Correct |
4 ms |
212 KB |
Output is correct |
36 |
Correct |
30 ms |
280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
4 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
212 KB |
Output is correct |
13 |
Correct |
29 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
224 KB |
Output is correct |
21 |
Correct |
6 ms |
212 KB |
Output is correct |
22 |
Correct |
6 ms |
212 KB |
Output is correct |
23 |
Correct |
6 ms |
308 KB |
Output is correct |
24 |
Correct |
4 ms |
212 KB |
Output is correct |
25 |
Correct |
6 ms |
304 KB |
Output is correct |
26 |
Correct |
27 ms |
212 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
3924 KB |
Output is correct |
29 |
Correct |
7 ms |
11220 KB |
Output is correct |
30 |
Correct |
10 ms |
14320 KB |
Output is correct |
31 |
Correct |
18 ms |
21820 KB |
Output is correct |
32 |
Correct |
17 ms |
21800 KB |
Output is correct |
33 |
Correct |
15 ms |
21860 KB |
Output is correct |
34 |
Correct |
18 ms |
21844 KB |
Output is correct |
35 |
Correct |
16 ms |
21844 KB |
Output is correct |
36 |
Correct |
20 ms |
21812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
4 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
212 KB |
Output is correct |
13 |
Correct |
29 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
3924 KB |
Output is correct |
16 |
Correct |
10 ms |
11220 KB |
Output is correct |
17 |
Correct |
9 ms |
14384 KB |
Output is correct |
18 |
Correct |
18 ms |
21876 KB |
Output is correct |
19 |
Correct |
16 ms |
21852 KB |
Output is correct |
20 |
Correct |
16 ms |
21876 KB |
Output is correct |
21 |
Correct |
17 ms |
21844 KB |
Output is correct |
22 |
Correct |
17 ms |
21844 KB |
Output is correct |
23 |
Correct |
21 ms |
21888 KB |
Output is correct |
24 |
Runtime error |
2 ms |
1160 KB |
Execution killed with signal 11 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
5 ms |
304 KB |
Output is correct |
9 |
Correct |
4 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
304 KB |
Output is correct |
13 |
Correct |
30 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
6 ms |
212 KB |
Output is correct |
22 |
Correct |
6 ms |
300 KB |
Output is correct |
23 |
Correct |
7 ms |
260 KB |
Output is correct |
24 |
Correct |
4 ms |
212 KB |
Output is correct |
25 |
Correct |
7 ms |
308 KB |
Output is correct |
26 |
Correct |
28 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
2 ms |
212 KB |
Output is correct |
31 |
Correct |
7 ms |
212 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
33 |
Correct |
28 ms |
212 KB |
Output is correct |
34 |
Correct |
30 ms |
212 KB |
Output is correct |
35 |
Correct |
4 ms |
212 KB |
Output is correct |
36 |
Correct |
30 ms |
212 KB |
Output is correct |
37 |
Correct |
2 ms |
2580 KB |
Output is correct |
38 |
Correct |
3 ms |
3924 KB |
Output is correct |
39 |
Correct |
8 ms |
11220 KB |
Output is correct |
40 |
Correct |
10 ms |
14308 KB |
Output is correct |
41 |
Correct |
19 ms |
21884 KB |
Output is correct |
42 |
Correct |
16 ms |
21884 KB |
Output is correct |
43 |
Correct |
15 ms |
21844 KB |
Output is correct |
44 |
Correct |
17 ms |
21844 KB |
Output is correct |
45 |
Correct |
17 ms |
21844 KB |
Output is correct |
46 |
Correct |
17 ms |
21840 KB |
Output is correct |
47 |
Runtime error |
2 ms |
1236 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
4 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
288 KB |
Output is correct |
13 |
Correct |
30 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
7 ms |
308 KB |
Output is correct |
19 |
Correct |
4 ms |
212 KB |
Output is correct |
20 |
Correct |
28 ms |
288 KB |
Output is correct |
21 |
Correct |
30 ms |
304 KB |
Output is correct |
22 |
Correct |
4 ms |
300 KB |
Output is correct |
23 |
Correct |
30 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
3 ms |
3924 KB |
Output is correct |
26 |
Correct |
8 ms |
11220 KB |
Output is correct |
27 |
Correct |
10 ms |
14292 KB |
Output is correct |
28 |
Correct |
19 ms |
21800 KB |
Output is correct |
29 |
Correct |
17 ms |
21832 KB |
Output is correct |
30 |
Correct |
16 ms |
21876 KB |
Output is correct |
31 |
Correct |
17 ms |
21844 KB |
Output is correct |
32 |
Correct |
19 ms |
22016 KB |
Output is correct |
33 |
Correct |
17 ms |
21920 KB |
Output is correct |
34 |
Runtime error |
3 ms |
1236 KB |
Execution killed with signal 11 |
35 |
Halted |
0 ms |
0 KB |
- |