#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
int n, A, B;
vector<long long> f;
long long a[N];
void read() {
cin >> n >> A >> B;
f.assign(n + 1, 0);
for(int i = 1; i <= n; i++) {
cin >> f[i];
a[i] = f[i];
f[i] += f[i - 1];
}
}
long long sum(int l, int r) {
return f[r] - f[l - 1];
}
void sub1() {
int m = n - 1;
long long ans = INF;
for(int mask = 0; mask < MASK(m); mask++) {
if (CNTBIT(mask) + 1 > B || CNTBIT(mask) + 1 < A) continue;
int pre = 1;
long long res = 0;
for(int i = 1; i < n; i++) if (GETBIT(mask, i - 1)) {
res |= sum(pre, i);
pre = i + 1;
}
res |= sum(pre, n);
ans = min(ans, res);
}
cout << ans;
}
void sub3() {
vector<vector<vector<long long>>> dp(n + 2, vector<vector<long long>>(B + 2));
dp[0][0].push_back(0);
for(int i = 1; i <= n; i++) {
for(int k = 1; k <= min(i, B); k++) {
long long x = 0;
for(int j = i; j >= 1; j--) {
x += a[j];
long long mn1 = INF, mn2 = INF, mn3 = INF;
for(long long z: dp[j - 1][k - 1]) {
long long tmp = (x | z);
if (mn1 > tmp) {
mn3 = mn2;
mn2 = mn1;
mn1 = tmp;
}
else if (mn2 > tmp) {
mn3 = mn2;
mn2 = tmp;
}
else minimize(mn3, tmp);
}
if (mn1 != INF) dp[i][k].push_back(mn1);
if (mn2 != INF) dp[i][k].push_back(mn2);
if (mn3 != INF) dp[i][k].push_back(mn3);
}
}
}
long long ans = INF;
for(int k = A; k <= B; k++) {
for(long long x: dp[n][k]) ans = min(ans, x);
}
cout << ans;
}
const int NUMBIT = 41;
bool compare(long long mask1, long long mask2, int x) {
for(int i = NUMBIT; i >= x; i--) if (!GETBIT(mask1, i)) {
if (GETBIT(mask2, i)) return false;
}
return true;
}
bool setZero1(long long cur, int bit) {
const pair<long long, long long> inf = mp(INF, INF);
vector<pair<long long, long long>> dp(n + 2, inf);
// dp[0][0] = {0, 0};
dp[0] = {0, 0}; // (group, sumOr)
for(int i = 1; i <= n; i++) {
long long val = 0;
for(int j = i; j >= 1; j--) {
val += a[j];
if (dp[j - 1] == inf) continue;
if (compare(cur, (dp[j - 1].se | val), bit)) {
minimize(dp[i], mp(dp[j - 1].fi + 1, dp[j - 1].se | val));
}
}
}
if (dp[n] == inf) return false;
if (dp[n].fi < A || dp[n].fi > B) return false;
return true;
}
bool setZero0(long long cur, int bit) {
vector<vector<long long>> dp(n + 2, vector<long long>(B + 2, INF));
dp[0][0] = 0;
for(int i = 1; i <= n; i++) {
for(int k = 1; k <= min(i, B); k++) {
long long val = 0;
for(int j = i; j >= 1; j--) {
val += a[j];
if (dp[j - 1][k - 1] == INF) continue;
if (compare(cur, (dp[j - 1][k - 1] | val), bit)) {
minimize(dp[i][k], dp[j - 1][k - 1] | val);
}
}
}
}
for(int k = A; k <= B; k++) if (dp[n][k] < INF) return true;
return false;
}
void sub4() {
long long cur = 0;
for(int i = 0; i <= NUMBIT; i++) cur |= MASK(i);
for(int i = NUMBIT; i >= 0; i--) {
cur ^= MASK(i);
if (A > 1) {
if (!setZero0(cur, i)) cur |= MASK(i);
}
else {
if (!setZero1(cur, i)) cur |= MASK(i);
}
}
cout << cur << endl;
}
void solve() {
sub4(); return;
if (n <= 20) {
sub1();
return;
}
else {
sub3();
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
// cerr << (1082353817592 | 53288572918018395) << endl;
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
Compilation message
sculpture.cpp: In function 'int main()':
sculpture.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sculpture.cpp:157:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
157 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
500 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
504 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
508 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
500 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
516 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
508 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
2 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
3 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
3 ms |
336 KB |
Output is correct |
27 |
Correct |
3 ms |
336 KB |
Output is correct |
28 |
Correct |
4 ms |
336 KB |
Output is correct |
29 |
Correct |
4 ms |
336 KB |
Output is correct |
30 |
Correct |
5 ms |
504 KB |
Output is correct |
31 |
Correct |
5 ms |
336 KB |
Output is correct |
32 |
Correct |
7 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
504 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
504 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
504 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
508 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
2 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
2 ms |
336 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
2 ms |
336 KB |
Output is correct |
46 |
Correct |
2 ms |
336 KB |
Output is correct |
47 |
Correct |
2 ms |
336 KB |
Output is correct |
48 |
Correct |
2 ms |
336 KB |
Output is correct |
49 |
Correct |
3 ms |
336 KB |
Output is correct |
50 |
Correct |
3 ms |
336 KB |
Output is correct |
51 |
Correct |
4 ms |
336 KB |
Output is correct |
52 |
Correct |
4 ms |
504 KB |
Output is correct |
53 |
Correct |
4 ms |
336 KB |
Output is correct |
54 |
Correct |
4 ms |
336 KB |
Output is correct |
55 |
Correct |
4 ms |
336 KB |
Output is correct |
56 |
Correct |
1 ms |
336 KB |
Output is correct |
57 |
Correct |
1 ms |
336 KB |
Output is correct |
58 |
Correct |
2 ms |
336 KB |
Output is correct |
59 |
Correct |
1 ms |
336 KB |
Output is correct |
60 |
Correct |
2 ms |
508 KB |
Output is correct |
61 |
Correct |
2 ms |
336 KB |
Output is correct |
62 |
Correct |
2 ms |
336 KB |
Output is correct |
63 |
Correct |
2 ms |
336 KB |
Output is correct |
64 |
Correct |
70 ms |
532 KB |
Output is correct |
65 |
Correct |
4 ms |
336 KB |
Output is correct |
66 |
Correct |
3 ms |
336 KB |
Output is correct |
67 |
Correct |
6 ms |
336 KB |
Output is correct |
68 |
Correct |
14 ms |
336 KB |
Output is correct |
69 |
Correct |
24 ms |
336 KB |
Output is correct |
70 |
Correct |
19 ms |
336 KB |
Output is correct |
71 |
Correct |
1 ms |
336 KB |
Output is correct |
72 |
Correct |
23 ms |
336 KB |
Output is correct |
73 |
Correct |
31 ms |
336 KB |
Output is correct |
74 |
Correct |
23 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
504 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
31 |
Correct |
3 ms |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
3 ms |
336 KB |
Output is correct |
37 |
Correct |
3 ms |
336 KB |
Output is correct |
38 |
Correct |
4 ms |
508 KB |
Output is correct |
39 |
Correct |
6 ms |
336 KB |
Output is correct |
40 |
Correct |
4 ms |
336 KB |
Output is correct |
41 |
Correct |
4 ms |
336 KB |
Output is correct |
42 |
Correct |
4 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
508 KB |
Output is correct |
44 |
Correct |
2 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
2 ms |
336 KB |
Output is correct |
48 |
Correct |
2 ms |
592 KB |
Output is correct |
49 |
Correct |
2 ms |
508 KB |
Output is correct |
50 |
Correct |
2 ms |
336 KB |
Output is correct |
51 |
Correct |
2 ms |
336 KB |
Output is correct |
52 |
Correct |
27 ms |
496 KB |
Output is correct |
53 |
Correct |
21 ms |
336 KB |
Output is correct |
54 |
Correct |
101 ms |
336 KB |
Output is correct |
55 |
Correct |
72 ms |
336 KB |
Output is correct |
56 |
Correct |
162 ms |
336 KB |
Output is correct |
57 |
Correct |
119 ms |
536 KB |
Output is correct |
58 |
Correct |
140 ms |
336 KB |
Output is correct |
59 |
Correct |
216 ms |
336 KB |
Output is correct |
60 |
Correct |
220 ms |
540 KB |
Output is correct |
61 |
Correct |
3 ms |
336 KB |
Output is correct |
62 |
Correct |
21 ms |
504 KB |
Output is correct |
63 |
Correct |
104 ms |
336 KB |
Output is correct |
64 |
Correct |
34 ms |
336 KB |
Output is correct |
65 |
Correct |
63 ms |
504 KB |
Output is correct |
66 |
Correct |
104 ms |
500 KB |
Output is correct |
67 |
Correct |
177 ms |
504 KB |
Output is correct |
68 |
Correct |
346 ms |
540 KB |
Output is correct |
69 |
Correct |
116 ms |
336 KB |
Output is correct |