//******************/
//* I<3 C++ */
//* I WANT ANY AC */
//* I LOVE PROGRAM!*/
//*IT'S long longERESTING*/
//* I LOVE PROGRAM!*/
//* IN CONTESTS */
//* GET SCORE */
//* AC CODE */
//* LET'S */
//* GO */
//* Written by: */
//* Duc Minh */
#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
#include <string>
#include <queue>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <deque>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define file(name) freopen(name".inp", "r", stdin);\
freopen(name".out", "w", stdout);
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define all(a) a.begin(),a.end()
#define endl "\n"
#define all1(a) a+1,a+n+1
#define unordered_map map
#define Accepted 0
// #define push_back emplace_back
// #define gcd(a,b) __gcd(a,b);
// #define lcm(a,b) (a*b)/gcd(a,b);
const long long INF = (long long)1e18;
const long long MOD = (long long)1e9+7;
const long long MODD = 14062008; /// 998244353
const long long maxN = 2e5+9;
const long long LOG = 57;
///--------------------------------
void solve();
signed main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
// file("tbrackets");
long long t;
// cin >> t;
t=1;
while (t--){
solve();
}
cerr << "Time elapsed: " << TIME << "s.\n";
cerr << "ducminh" << "\n";
return Accepted;
}
///--------------------[PROBLEM SOLUTION]--------------------///
long long a[2009], dp[2009][2009], pre[2009];
long long ans=INF,n,L,R;
void sub1(){
for (long long mask=0; mask<(1<<n); mask++){
long long sum=0,g=0;
for (long long i=0; i<n; i++){
sum+=a[i];
if ((mask>>i)&1){
g|=sum;
sum=0;
}
}
g|=sum;
long long bits=__builtin_popcount(mask) + ((mask & (1 << (n - 1))) == 0);
if (L<=bits && bits<=R) ans=min(ans,g);
}
cout << ans;
exit(0);
}
void sub2(){
long long dp[59][29][509];
memset(dp, false ,sizeof dp);
dp[0][0][0] = true;
const int N=500;
for (int i=1; i<=n; i++){
pre[i]=pre[i-1]+a[i-1];
}
for (long long i=0; i<n; i++){
for (long long k=0; k<=min(i,R-1); k++){
for (long long j=0; j<=N; j++){
if (!dp[i][k][j]) continue;
for (long long t=i+1; t<=n; t++){
dp[t][k+1][j|(pre[t]-pre[i])] |= dp[i][k][j];
}
}
}
}
for (long long j = L ; j <= R ; j++)
for (long long t = 0; t <= N; t++){
if (dp[n][j][t]) ans=min(ans,t);
}
cout << ans;
exit(0);
}
void sub3(){
const int N=2000;
for (long long i=0; i<=n+1; i++){
// pre[i]=0;
for (long long j=0; j<=N+1; j++){
dp[i][j]=INF;
}
}
// sort(a,a+n);
// for (int i=1; i<=n; i++){
// pre[i]=pre[i-1]+a[i];
// // cout << pre[i] << ' ';
// }
dp[0][0]=0;
// dp[i][j][or]
for (long long i=0; i<n; i++){
for (int k=0; k<=N; k++){
for (int j=i; j<n; j++){
dp[j+1][k|(pre[j]-pre[i-1])]=min(dp[j+1][k|(pre[j]-pre[i-1])],dp[i][k]+1);
// cout << i << ' ' << j << ' ' << dp[i][k] << "\n";
}
}
}
for (int k=0; k<=N; k++){
// cout << dp[n-1][k] << ' ';
if (dp[n][k]<=R) {cout << k; exit(0);}
}
// cout << dp[n][5];
// exit(0);
}
bool checksub4(){
long long dp[2009][2009];
memset(dp, false ,sizeof dp);
dp[0][0] = true;
const int N=500;
// for (int i=1; i<=n; i++){
// pre[i]=pre[i-1]+a[i-1];
// }
for (long long i=0; i<n; i++){
for (long long k=0; k<=min(i,R-1); k++){
// if (!dp[i][k]) continue;
long long sum=0;
for (long long t=i; t<=n; t++){
sum+=a[t];
if ((sum|ans)==ans) dp[t+1][k+1] |= dp[i][k];
}
}
}
for (long long j = L ; j <= R ; j++)
if (dp[n][j]) return true;
return false;
exit(0);
}
void sub4(){
ans = (1LL<<LOG) - 1;
for (long long i=LOG-1; i>=0; i--){
ans^=1LL<<i;
if (!checksub4()){
ans|=1LL<<i;
}
}
cout << ans;
exit(0);
}
bool checksub5(){
long long dp[20009];
for (int i = 0; i <= n; i++) {
dp[i] = 100000;
}
dp[0] = 0;
const int N=500;
// for (int i=1; i<=n; i++){
// pre[i]=pre[i-1]+a[i-1];
// }
for (long long i=0; i<n; i++){
if (dp[i]>=100000) continue;
// if (!dp[i][k]) continue;
long long sum=0;
for (long long t=i; t<n; t++){
sum+=a[t];
if ((sum|ans)==ans) dp[t+1] = min(dp[t+1],dp[i]+1);
}
}
return dp[n]<=R;
exit(0);
}
void sub5(){
ans = (1LL<<LOG) - 1;
for (long long i=LOG-1; i>=0; i--){
ans^=1LL<<i;
if (!checksub5()){
ans|=1LL<<i;
}
}
cout << ans;
exit(0);
}
void solve(){
cin >> n >> L >> R;
long long mx=-1e9;
for (long long i=0; i<n; i++){
cin >> a[i]; if (i==0) pre[i]=a[i]; else pre[i]=pre[i-1]+a[i];
mx=max(mx,a[i]);
}
// cout << pre[2]-pre[0] << "\n";
if (n<=20) sub1();
if (n<=50 && R<=min(20LL,n)) sub2();
if (n<=100 && L==1 && mx<=3000) sub3();
if (n<=100 && mx<=1000000000) sub4();
sub5();
}
Compilation message
sculpture.cpp: In function 'bool checksub4()':
sculpture.cpp:250:15: warning: unused variable 'N' [-Wunused-variable]
250 | const int N=500;
| ^
sculpture.cpp: In function 'bool checksub5()':
sculpture.cpp:323:15: warning: unused variable 'N' [-Wunused-variable]
323 | const int N=500;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
8 ms |
464 KB |
Output is correct |
8 |
Correct |
65 ms |
440 KB |
Output is correct |
9 |
Correct |
67 ms |
348 KB |
Output is correct |
10 |
Correct |
66 ms |
344 KB |
Output is correct |
11 |
Correct |
67 ms |
348 KB |
Output is correct |
12 |
Correct |
65 ms |
348 KB |
Output is correct |
13 |
Correct |
65 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
8 ms |
464 KB |
Output is correct |
21 |
Correct |
65 ms |
456 KB |
Output is correct |
22 |
Correct |
62 ms |
440 KB |
Output is correct |
23 |
Correct |
64 ms |
440 KB |
Output is correct |
24 |
Correct |
65 ms |
348 KB |
Output is correct |
25 |
Correct |
62 ms |
348 KB |
Output is correct |
26 |
Correct |
63 ms |
436 KB |
Output is correct |
27 |
Correct |
0 ms |
360 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
32 ms |
472 KB |
Output is correct |
31 |
Correct |
62 ms |
348 KB |
Output is correct |
32 |
Correct |
65 ms |
444 KB |
Output is correct |
33 |
Correct |
63 ms |
348 KB |
Output is correct |
34 |
Correct |
68 ms |
592 KB |
Output is correct |
35 |
Correct |
65 ms |
344 KB |
Output is correct |
36 |
Correct |
66 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
65 ms |
348 KB |
Output is correct |
9 |
Correct |
67 ms |
436 KB |
Output is correct |
10 |
Correct |
65 ms |
348 KB |
Output is correct |
11 |
Correct |
65 ms |
440 KB |
Output is correct |
12 |
Correct |
65 ms |
348 KB |
Output is correct |
13 |
Correct |
65 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
8 ms |
464 KB |
Output is correct |
21 |
Correct |
62 ms |
348 KB |
Output is correct |
22 |
Correct |
63 ms |
344 KB |
Output is correct |
23 |
Correct |
62 ms |
344 KB |
Output is correct |
24 |
Correct |
65 ms |
348 KB |
Output is correct |
25 |
Correct |
63 ms |
344 KB |
Output is correct |
26 |
Correct |
63 ms |
344 KB |
Output is correct |
27 |
Correct |
5 ms |
7260 KB |
Output is correct |
28 |
Correct |
5 ms |
7164 KB |
Output is correct |
29 |
Correct |
4 ms |
7260 KB |
Output is correct |
30 |
Correct |
5 ms |
7256 KB |
Output is correct |
31 |
Correct |
5 ms |
7260 KB |
Output is correct |
32 |
Correct |
5 ms |
7256 KB |
Output is correct |
33 |
Correct |
5 ms |
7256 KB |
Output is correct |
34 |
Correct |
5 ms |
7260 KB |
Output is correct |
35 |
Correct |
5 ms |
7276 KB |
Output is correct |
36 |
Correct |
5 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
8 ms |
348 KB |
Output is correct |
8 |
Correct |
65 ms |
436 KB |
Output is correct |
9 |
Correct |
65 ms |
348 KB |
Output is correct |
10 |
Correct |
66 ms |
444 KB |
Output is correct |
11 |
Correct |
65 ms |
444 KB |
Output is correct |
12 |
Correct |
65 ms |
436 KB |
Output is correct |
13 |
Correct |
67 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
7260 KB |
Output is correct |
15 |
Correct |
5 ms |
7260 KB |
Output is correct |
16 |
Correct |
8 ms |
7516 KB |
Output is correct |
17 |
Correct |
4 ms |
7260 KB |
Output is correct |
18 |
Correct |
5 ms |
7260 KB |
Output is correct |
19 |
Correct |
5 ms |
7260 KB |
Output is correct |
20 |
Correct |
5 ms |
7260 KB |
Output is correct |
21 |
Correct |
5 ms |
7260 KB |
Output is correct |
22 |
Correct |
5 ms |
7260 KB |
Output is correct |
23 |
Correct |
5 ms |
7260 KB |
Output is correct |
24 |
Correct |
4 ms |
2648 KB |
Output is correct |
25 |
Correct |
6 ms |
2648 KB |
Output is correct |
26 |
Correct |
9 ms |
2760 KB |
Output is correct |
27 |
Correct |
13 ms |
2676 KB |
Output is correct |
28 |
Correct |
14 ms |
2652 KB |
Output is correct |
29 |
Correct |
15 ms |
2652 KB |
Output is correct |
30 |
Correct |
14 ms |
2648 KB |
Output is correct |
31 |
Correct |
15 ms |
2652 KB |
Output is correct |
32 |
Correct |
14 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
8 ms |
460 KB |
Output is correct |
8 |
Correct |
65 ms |
348 KB |
Output is correct |
9 |
Correct |
65 ms |
348 KB |
Output is correct |
10 |
Correct |
65 ms |
348 KB |
Output is correct |
11 |
Correct |
65 ms |
348 KB |
Output is correct |
12 |
Correct |
67 ms |
348 KB |
Output is correct |
13 |
Correct |
65 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
544 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
8 ms |
348 KB |
Output is correct |
21 |
Correct |
62 ms |
348 KB |
Output is correct |
22 |
Correct |
62 ms |
348 KB |
Output is correct |
23 |
Correct |
62 ms |
344 KB |
Output is correct |
24 |
Correct |
67 ms |
452 KB |
Output is correct |
25 |
Correct |
62 ms |
348 KB |
Output is correct |
26 |
Correct |
64 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
32 ms |
436 KB |
Output is correct |
31 |
Correct |
64 ms |
344 KB |
Output is correct |
32 |
Correct |
65 ms |
344 KB |
Output is correct |
33 |
Correct |
62 ms |
348 KB |
Output is correct |
34 |
Correct |
65 ms |
348 KB |
Output is correct |
35 |
Correct |
65 ms |
344 KB |
Output is correct |
36 |
Correct |
66 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
7256 KB |
Output is correct |
38 |
Correct |
5 ms |
7256 KB |
Output is correct |
39 |
Correct |
6 ms |
7260 KB |
Output is correct |
40 |
Correct |
5 ms |
7260 KB |
Output is correct |
41 |
Correct |
5 ms |
7260 KB |
Output is correct |
42 |
Correct |
5 ms |
7256 KB |
Output is correct |
43 |
Correct |
5 ms |
7260 KB |
Output is correct |
44 |
Correct |
5 ms |
7260 KB |
Output is correct |
45 |
Correct |
6 ms |
7260 KB |
Output is correct |
46 |
Correct |
5 ms |
7084 KB |
Output is correct |
47 |
Correct |
5 ms |
2652 KB |
Output is correct |
48 |
Correct |
7 ms |
2676 KB |
Output is correct |
49 |
Correct |
9 ms |
2652 KB |
Output is correct |
50 |
Correct |
12 ms |
2648 KB |
Output is correct |
51 |
Correct |
15 ms |
2684 KB |
Output is correct |
52 |
Correct |
16 ms |
2684 KB |
Output is correct |
53 |
Correct |
14 ms |
2652 KB |
Output is correct |
54 |
Correct |
15 ms |
2652 KB |
Output is correct |
55 |
Correct |
14 ms |
2652 KB |
Output is correct |
56 |
Correct |
216 ms |
32028 KB |
Output is correct |
57 |
Correct |
216 ms |
32028 KB |
Output is correct |
58 |
Correct |
219 ms |
31832 KB |
Output is correct |
59 |
Correct |
219 ms |
31836 KB |
Output is correct |
60 |
Correct |
218 ms |
31832 KB |
Output is correct |
61 |
Correct |
204 ms |
32032 KB |
Output is correct |
62 |
Correct |
217 ms |
32080 KB |
Output is correct |
63 |
Correct |
220 ms |
31836 KB |
Output is correct |
64 |
Correct |
217 ms |
32028 KB |
Output is correct |
65 |
Correct |
213 ms |
32028 KB |
Output is correct |
66 |
Correct |
199 ms |
31832 KB |
Output is correct |
67 |
Correct |
212 ms |
31836 KB |
Output is correct |
68 |
Correct |
217 ms |
32028 KB |
Output is correct |
69 |
Correct |
217 ms |
31832 KB |
Output is correct |
70 |
Correct |
223 ms |
32032 KB |
Output is correct |
71 |
Correct |
210 ms |
31908 KB |
Output is correct |
72 |
Correct |
215 ms |
32028 KB |
Output is correct |
73 |
Correct |
213 ms |
32080 KB |
Output is correct |
74 |
Correct |
210 ms |
31836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
8 ms |
348 KB |
Output is correct |
8 |
Correct |
65 ms |
596 KB |
Output is correct |
9 |
Correct |
65 ms |
344 KB |
Output is correct |
10 |
Correct |
65 ms |
348 KB |
Output is correct |
11 |
Correct |
66 ms |
444 KB |
Output is correct |
12 |
Correct |
65 ms |
440 KB |
Output is correct |
13 |
Correct |
65 ms |
440 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
32 ms |
348 KB |
Output is correct |
18 |
Correct |
62 ms |
436 KB |
Output is correct |
19 |
Correct |
65 ms |
344 KB |
Output is correct |
20 |
Correct |
65 ms |
436 KB |
Output is correct |
21 |
Correct |
66 ms |
344 KB |
Output is correct |
22 |
Correct |
65 ms |
348 KB |
Output is correct |
23 |
Correct |
65 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
7260 KB |
Output is correct |
25 |
Correct |
5 ms |
7260 KB |
Output is correct |
26 |
Correct |
4 ms |
7248 KB |
Output is correct |
27 |
Correct |
5 ms |
7260 KB |
Output is correct |
28 |
Correct |
5 ms |
7260 KB |
Output is correct |
29 |
Correct |
5 ms |
7260 KB |
Output is correct |
30 |
Correct |
5 ms |
7256 KB |
Output is correct |
31 |
Correct |
5 ms |
7256 KB |
Output is correct |
32 |
Correct |
5 ms |
7260 KB |
Output is correct |
33 |
Correct |
5 ms |
7260 KB |
Output is correct |
34 |
Correct |
5 ms |
2652 KB |
Output is correct |
35 |
Correct |
6 ms |
2904 KB |
Output is correct |
36 |
Correct |
9 ms |
2652 KB |
Output is correct |
37 |
Correct |
12 ms |
2652 KB |
Output is correct |
38 |
Correct |
15 ms |
2652 KB |
Output is correct |
39 |
Correct |
14 ms |
2680 KB |
Output is correct |
40 |
Correct |
15 ms |
2652 KB |
Output is correct |
41 |
Correct |
14 ms |
2652 KB |
Output is correct |
42 |
Correct |
14 ms |
2652 KB |
Output is correct |
43 |
Correct |
212 ms |
32032 KB |
Output is correct |
44 |
Correct |
210 ms |
31844 KB |
Output is correct |
45 |
Correct |
216 ms |
31836 KB |
Output is correct |
46 |
Correct |
222 ms |
32036 KB |
Output is correct |
47 |
Correct |
220 ms |
32080 KB |
Output is correct |
48 |
Correct |
205 ms |
32024 KB |
Output is correct |
49 |
Correct |
222 ms |
32024 KB |
Output is correct |
50 |
Correct |
213 ms |
32024 KB |
Output is correct |
51 |
Correct |
1 ms |
604 KB |
Output is correct |
52 |
Correct |
6 ms |
604 KB |
Output is correct |
53 |
Correct |
8 ms |
604 KB |
Output is correct |
54 |
Correct |
23 ms |
652 KB |
Output is correct |
55 |
Correct |
20 ms |
852 KB |
Output is correct |
56 |
Correct |
52 ms |
604 KB |
Output is correct |
57 |
Correct |
47 ms |
604 KB |
Output is correct |
58 |
Correct |
57 ms |
604 KB |
Output is correct |
59 |
Correct |
68 ms |
800 KB |
Output is correct |
60 |
Correct |
54 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
8 ms |
636 KB |
Output is correct |
63 |
Correct |
24 ms |
604 KB |
Output is correct |
64 |
Correct |
13 ms |
604 KB |
Output is correct |
65 |
Correct |
24 ms |
656 KB |
Output is correct |
66 |
Correct |
36 ms |
604 KB |
Output is correct |
67 |
Correct |
47 ms |
600 KB |
Output is correct |
68 |
Correct |
87 ms |
600 KB |
Output is correct |
69 |
Correct |
49 ms |
600 KB |
Output is correct |