Submission #1324051

#TimeUsernameProblemLanguageResultExecution timeMemory
1324051sh_qaxxorov_571Bali Sculptures (APIO15_sculpture)C++20
100 / 100
149 ms456 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

int N, A, B;
ll Y[2005];
ll prefix_sum[2005];

// Subtask 4 uchun: A va B cheklovi bilan tekshirish
bool check_with_AB(ll current_ans) {
    bool dp[105][105] = {false};
    dp[0][0] = true;

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= i; j++) {
            for (int k = 0; k < i; k++) {
                ll sum = prefix_sum[i] - prefix_sum[k];
                // Agar sum joriy "ans" maskasiga mos kelsa
                if (dp[k][j - 1] && ((sum | current_ans) == current_ans)) {
                    dp[i][j] = true;
                }
            }
        }
    }

    for (int j = A; j <= B; j++) {
        if (dp[N][j]) return true;
    }
    return false;
}

// Subtask 5 uchun: Faqat B cheklovi (A=1) bilan tekshirish
bool check_with_B(ll current_ans) {
    int dp[2005]; // dp[i] - minimal guruhlar soni
    fill(dp, dp + 2005, N + 1);
    dp[0] = 0;

    for (int i = 1; i <= N; i++) {
        for (int k = 0; k < i; k++) {
            ll sum = prefix_sum[i] - prefix_sum[k];
            if ((sum | current_ans) == current_ans) {
                dp[i] = min(dp[i], dp[k] + 1);
            }
        }
    }
    return dp[N] <= B;
}

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

    cin >> N >> A >> B;
    for (int i = 1; i <= N; i++) {
        cin >> Y[i];
        prefix_sum[i] = prefix_sum[i - 1] + Y[i];
    }

    ll ans = (1LL << 62) - 1; // Barcha bitlar 1 dan boshlanadi

    // Eng yuqori bitdan (61) boshlab pastga qarab harakat qilamiz
    for (int bit = 61; bit >= 0; bit--) {
        ll temp_ans = ans ^ (1LL << bit); // Ushbu bitni 0 qilib ko'ramiz
        
        bool possible;
        if (A == 1) possible = check_with_B(temp_ans);
        else possible = check_with_AB(temp_ans);

        if (possible) {
            ans = temp_ans; // Agar bitni 0 qilish iloji bo'lsa, natijani yangilaymiz
        }
    }

    cout << ans << endl;

    return 0;
}
#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...