#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;
vector<ll> year(n+1);
for(ll i = 1; i <= n; i++){
cin >> year[i];
}
// Precompute prefix sums
vector<ll> prefix(n+1, 0);
for(ll i = 1; i <= n; i++){
prefix[i] = prefix[i-1] + year[i];
}
ll ans = 0;
for(ll digit = bitsize; digit >= 0; digit--){
if(a == 1){
// Case when a == 1
vector<ll> dp(n+1, 1e18);
dp[0] = 0;
for(ll i = 0; i <= n; i++){
for(ll j = i+1; j <= n; j++){
ll sum = prefix[j] - prefix[i];
bool valid = true;
// Check if higher bits are compatible with ans
for(ll k = digit+1; k <= bitsize; k++){
if(((sum >> k) & 1) && !((ans >> k) & 1)){
valid = false;
break;
}
}
// Check if the current bit can be 0
if((sum >> digit) & 1){
valid = false;
}
if(valid){
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
vector<vector<ll>> dp(n+1, vector<ll>(n+1, 1e18));
dp[0][0] = 0;
for(ll i = 0; i <= n; i++){
for(ll j = i+1; j <= n; j++){
ll sum = prefix[j] - prefix[i];
bool valid = true;
// Check if higher bits are compatible with ans
for(ll k = digit+1; k <= bitsize; k++){
if(((sum >> k) & 1) && !((ans >> k) & 1)){
valid = false;
break;
}
}
// Check if the current bit can be 0
if((sum >> digit) & 1){
valid = false;
}
if(valid){
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... |