#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll bitsize = 40; // Increased bit size
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, a, b;
cin >> n >> a >> b;
ll year[n+5];
for(ll i = 1; i <= n; i++){
cin >> year[i];
}
ll sumyear[n+5][n+5];
for(ll l = 1; l <= n; ++l){
ll sumnow = 0;
for(ll r = l; r <= n; ++r){
sumnow += year[r];
sumyear[l][r] = sumnow;
}
}
ll ans = 0;
for(ll digit = bitsize; digit >= 0; digit--){
if(a == 1){
// Case when a == 1
ll dp[n+5];
for(ll i = 0; i <= n; i++) dp[i] = 1e18;
dp[0] = 0;
for(ll i = 0; i <= n; i++){
for(ll j = i+1; j <= n; j++){
ll thissum = sumyear[i+1][j];
bool pass = true;
// Check if higher bits are compatible with ans
for(ll k = digit+1; k <= bitsize; k++){
if(((thissum >> k) & 1) && !((ans >> k) & 1)){
pass = false;
break;
}
}
// Check if the current bit can be 0
if((thissum >> digit) & 1){
pass = false;
}
if(pass){
dp[j] = min(dp[j], dp[i] + 1);
}
}
}
// If no valid partition exists, set this bit in ans
if(dp[n] > b){
ans += (1 << digit);
}
}
else{
// Case when a > 1
ll dp[n+5][n+5];
for(ll i = 0; i <= n; i++) for(ll j = 0; j <= n; j++) dp[i][j] = 1e18;
dp[0][0] = 0;
for(ll i = 0; i <= n; i++){
for(ll j = i+1; j <= n; j++){
ll thissum = sumyear[i+1][j];
bool pass = true;
// Check if higher bits are compatible with ans
for(ll k = digit+1; k <= bitsize; k++){
if(((thissum >> k) & 1) && !((ans >> k) & 1)){
pass = false;
break;
}
}
// Check if the current bit can be 0
if((thissum >> digit) & 1){
pass = false;
}
if(pass){
for(ll k = 0; k < n; k++){
dp[j][k+1] = min(dp[j][k+1], dp[i][k] + 1);
}
}
}
}
// Check if any partition count between a and b is valid
bool canSet = false;
for(ll k = a; k <= b; k++){
if(dp[n][k] <= b){
canSet = true;
break;
}
}
// If no valid partition exists, set this bit in ans
if(!canSet){
ans += (1 << digit);
}
}
}
cout << ans;
}
# | 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... |