Submission #705114

#TimeUsernameProblemLanguageResultExecution timeMemory
705114bebraBali Sculptures (APIO15_sculpture)C++17
50 / 100
110 ms336 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define dbg(x) cerr << #x << ": " << x << endl;

const int INF = 1e9;
const int MAX_BIT = 44;
const int MAX_N = 2000 + 5;
const int MAX_X = 20;
int a[MAX_N];


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, l, r;
    cin >> n >> l >> r;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    // minimize (a1 + a2 + ... + a_(k - 1)) | (a_k + a_(k + 1) + ...) | ... 
    // IMPORTANT NOTE: IN LAST GROUP n = 2000 and l = 1
    // maybe greedy bit by bit? minimize first, then second, ...

    if (l == 1) {
        auto check = [&](ll mask) {
            vector<int> dp(n + 1, INF);
            dp[0] = 0;
            for (int i = 1; i <= n; ++i) {
                ll s = 0;
                for (int j = i; j > 0; --j) {
                    s += a[j];
                    if ((s & mask) == 0) {
                        dp[i] = min(dp[i], dp[j - 1] + 1);
                    }
                }
            }
            return dp[n] <= r;
        };

        ll ans = 0;
        ll mask = 0;
        for (int bit = MAX_BIT; bit >= 0; --bit) {
            if (!check(mask | (1LL << bit))) {
                ans |= 1LL << bit;
            } else {
                mask |= 1LL << bit;
            }
        }
        cout << ans << '\n';
    } else {
        auto check = [&](ll mask) {
            vector<vector<bool>> dp(n + 1, vector<bool>(n + 1, INF));
            dp[0][0] = 1;
            for (int i = 1; i <= n; ++i) {
                ll s = 0;
                for (int j = i; j > 0; --j) {
                    s += a[j];
                    if ((s & mask) == 0) {
                        for (int k = 0; k <= j - 1; ++k) {
                            dp[i][k + 1] = dp[i][k + 1] | dp[j][k];
                        }
                    }
                }
            }
            for (int x = l; x <= r; ++x) {
                if (dp[n][x]) {
                    return true;
                }
            }
            return false;
        };

        ll ans = 0;
        ll mask = 0;
        for (int bit = MAX_BIT; bit >= 0; --bit) {
            if (!check(mask | (1LL << bit))) {
                ans |= 1LL << bit;
            } else {
                mask |= 1LL << bit;
            }
        }
        cout << ans << '\n';

    }

    return 0;
}

/*
6 1 3
8 1 2 1 5 4
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...