#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#define sz(v) (int)(v).size()
typedef long long ll;
const int MAXN = 101;
const int MAXV = 20;
const int INF = 1e9;
const ll INFLL = 1e18;
int dpMin[MAXN][MAXN*MAXV];
int dpMax[MAXN][MAXN*MAXV];
int arr[MAXN];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, l, r; cin >> n >> l >> r;
for (int i=1; i<=n; i++) cin >> arr[i];
for (int i=1; i<=n*MAXV; i++) dpMin[0][i] = INF;
dpMin[0][0] = 0;
for (int i=1; i<=n; i++) {
for (int cost=0; cost<=i*MAXV; cost++) {
dpMin[i][cost] = INF;
int sum = 0;
for (int j=i; j>=1; j--) {
sum += arr[j];
if ((sum & cost) != sum) continue;
for (int lastCost=0; lastCost<=(j-1)*MAXV; lastCost++) {
if ((lastCost | sum) != cost) continue;
dpMin[i][cost] = min(
dpMin[i][cost],
dpMin[j-1][lastCost] + 1
);
}
}
}
}
for (int i=1; i<=n*MAXV; i++) dpMax[0][i] = -INF;
dpMax[0][0] = 0;
for (int i=1; i<=n; i++) {
for (int cost=0; cost<=i*MAXV; cost++) {
dpMax[i][cost] = -INF;
int sum = 0;
for (int j=i; j>=1; j--) {
sum += arr[j];
if ((sum & cost) != sum) continue;
for (int lastCost=0; lastCost<=(j-1)*MAXV; lastCost++) {
if ((lastCost | sum) != cost) continue;
dpMax[i][cost] = max(
dpMax[i][cost],
dpMax[j-1][lastCost] + 1
);
}
}
}
}
for (int cost=0; cost<=n*MAXV; cost++) {
int mn = dpMin[n][cost], mx = dpMax[n][cost];
// cout << mn << ' ' << mx << endl;
if ((l <= mn and r >= mn) or (l >= mn and mx >= l)) {
cout << cost << '\n';
break;
}
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |