#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;
int 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<int>>> dp(n + 2, vector<vector<int>>(B + 2, vector<int>(n * 20 + 1, (int)1e9)));
const int inf = (int)1e9;
dp[0][0][0] = 0;
for(int i = 1; i <= n; i++) {
for(int k = 1; k <= min(i, B); k++) {
int x = 0;
for(int j = i; j >= 1; j--) {
x += a[j];
for(int z = 0; z <= n * 20; z++) {
if (dp[j - 1][k - 1][z] != inf) minimize(dp[i][k][x], (x | dp[j - 1][k - 1][z]));
}
}
}
}
int ans = inf;
for(int k = A; k <= B; k++) {
for(int x = 0; x <= n * 20; x++) ans = min(ans, dp[n][k][x]);
}
cout << ans;
}
void solve() {
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);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
Compilation message
sculpture.cpp: In function 'int main()':
sculpture.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sculpture.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
508 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
336 KB |
Output is correct |
13 |
Correct |
31 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 |
4 ms |
336 KB |
Output is correct |
22 |
Correct |
4 ms |
336 KB |
Output is correct |
23 |
Correct |
4 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
4 ms |
336 KB |
Output is correct |
26 |
Correct |
26 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 |
4 ms |
472 KB |
Output is correct |
32 |
Correct |
2 ms |
336 KB |
Output is correct |
33 |
Correct |
26 ms |
336 KB |
Output is correct |
34 |
Correct |
31 ms |
624 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
31 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
336 KB |
Output is correct |
13 |
Correct |
31 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 |
4 ms |
336 KB |
Output is correct |
22 |
Correct |
4 ms |
508 KB |
Output is correct |
23 |
Correct |
4 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
4 ms |
336 KB |
Output is correct |
26 |
Correct |
26 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
1104 KB |
Output is correct |
28 |
Correct |
2 ms |
1104 KB |
Output is correct |
29 |
Correct |
3 ms |
1104 KB |
Output is correct |
30 |
Correct |
13 ms |
3580 KB |
Output is correct |
31 |
Correct |
31 ms |
4944 KB |
Output is correct |
32 |
Correct |
28 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
848 KB |
Output is correct |
34 |
Correct |
20 ms |
5080 KB |
Output is correct |
35 |
Correct |
18 ms |
4432 KB |
Output is correct |
36 |
Correct |
11 ms |
2896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
3 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
336 KB |
Output is correct |
13 |
Correct |
31 ms |
336 KB |
Output is correct |
14 |
Correct |
3 ms |
1104 KB |
Output is correct |
15 |
Correct |
2 ms |
1104 KB |
Output is correct |
16 |
Correct |
4 ms |
1104 KB |
Output is correct |
17 |
Correct |
13 ms |
3472 KB |
Output is correct |
18 |
Correct |
33 ms |
4944 KB |
Output is correct |
19 |
Correct |
23 ms |
5112 KB |
Output is correct |
20 |
Correct |
2 ms |
1104 KB |
Output is correct |
21 |
Correct |
21 ms |
4944 KB |
Output is correct |
22 |
Correct |
20 ms |
4456 KB |
Output is correct |
23 |
Correct |
11 ms |
2896 KB |
Output is correct |
24 |
Correct |
22 ms |
5200 KB |
Output is correct |
25 |
Correct |
25 ms |
4660 KB |
Output is correct |
26 |
Correct |
89 ms |
12880 KB |
Output is correct |
27 |
Correct |
322 ms |
38728 KB |
Output is correct |
28 |
Correct |
586 ms |
83016 KB |
Output is correct |
29 |
Correct |
593 ms |
83108 KB |
Output is correct |
30 |
Correct |
10 ms |
2896 KB |
Output is correct |
31 |
Correct |
360 ms |
40016 KB |
Output is correct |
32 |
Correct |
525 ms |
61932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
336 KB |
Output is correct |
13 |
Correct |
31 ms |
468 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 |
4 ms |
336 KB |
Output is correct |
22 |
Correct |
4 ms |
336 KB |
Output is correct |
23 |
Correct |
4 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
4 ms |
336 KB |
Output is correct |
26 |
Correct |
26 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
504 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
4 ms |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
336 KB |
Output is correct |
33 |
Correct |
26 ms |
336 KB |
Output is correct |
34 |
Correct |
34 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
33 ms |
508 KB |
Output is correct |
37 |
Correct |
2 ms |
1272 KB |
Output is correct |
38 |
Correct |
3 ms |
1104 KB |
Output is correct |
39 |
Correct |
5 ms |
1104 KB |
Output is correct |
40 |
Correct |
12 ms |
3468 KB |
Output is correct |
41 |
Correct |
22 ms |
5112 KB |
Output is correct |
42 |
Correct |
20 ms |
4944 KB |
Output is correct |
43 |
Correct |
2 ms |
1104 KB |
Output is correct |
44 |
Correct |
21 ms |
4944 KB |
Output is correct |
45 |
Correct |
20 ms |
4432 KB |
Output is correct |
46 |
Correct |
12 ms |
2896 KB |
Output is correct |
47 |
Correct |
22 ms |
5264 KB |
Output is correct |
48 |
Correct |
24 ms |
4432 KB |
Output is correct |
49 |
Correct |
93 ms |
12880 KB |
Output is correct |
50 |
Correct |
307 ms |
38736 KB |
Output is correct |
51 |
Correct |
545 ms |
83016 KB |
Output is correct |
52 |
Correct |
557 ms |
82928 KB |
Output is correct |
53 |
Correct |
9 ms |
2896 KB |
Output is correct |
54 |
Correct |
347 ms |
40264 KB |
Output is correct |
55 |
Correct |
509 ms |
62048 KB |
Output is correct |
56 |
Runtime error |
12 ms |
10320 KB |
Execution killed with signal 11 |
57 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
4 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
336 KB |
Output is correct |
13 |
Correct |
31 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 |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
4 ms |
464 KB |
Output is correct |
19 |
Correct |
2 ms |
508 KB |
Output is correct |
20 |
Correct |
26 ms |
336 KB |
Output is correct |
21 |
Correct |
31 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
35 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
1104 KB |
Output is correct |
25 |
Correct |
2 ms |
1104 KB |
Output is correct |
26 |
Correct |
4 ms |
1104 KB |
Output is correct |
27 |
Correct |
12 ms |
3408 KB |
Output is correct |
28 |
Correct |
23 ms |
5088 KB |
Output is correct |
29 |
Correct |
22 ms |
4944 KB |
Output is correct |
30 |
Correct |
3 ms |
848 KB |
Output is correct |
31 |
Correct |
23 ms |
4944 KB |
Output is correct |
32 |
Correct |
21 ms |
4432 KB |
Output is correct |
33 |
Correct |
12 ms |
2896 KB |
Output is correct |
34 |
Correct |
33 ms |
5260 KB |
Output is correct |
35 |
Correct |
25 ms |
4432 KB |
Output is correct |
36 |
Correct |
88 ms |
12880 KB |
Output is correct |
37 |
Correct |
301 ms |
38836 KB |
Output is correct |
38 |
Correct |
599 ms |
83016 KB |
Output is correct |
39 |
Correct |
591 ms |
83016 KB |
Output is correct |
40 |
Correct |
10 ms |
2896 KB |
Output is correct |
41 |
Correct |
368 ms |
40016 KB |
Output is correct |
42 |
Correct |
505 ms |
62048 KB |
Output is correct |
43 |
Runtime error |
10 ms |
10320 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |