Submission #742320

#TimeUsernameProblemLanguageResultExecution timeMemory
742320viwlesxqBali Sculptures (APIO15_sculpture)C++17
46 / 100
771 ms10520 KiB
#include <bits/stdc++.h>

using namespace std;

typedef int64_t ll;
typedef string str;

const int N = 101;
const int R = 101;
const int Y = 2001;
const ll inf = 1e18;

ll y[N], pref[N];
bool dp[N][R][Y];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, a, b;
    cin >> n >> a >> b;
    if (n <= 20) {
        ll ans = inf;
        for (int i = 0; i < n; ++i) {
            cin >> y[i];
        }
        for (int mask = 0; mask < (1 << (n - 1)); ++mask) {
            int cnt = __builtin_popcount(mask) + 1;
            if (cnt < a || cnt > b) {
                continue;
            }
            ll cur = 0;
            ll sum = y[0];
            for (int bit = 0; bit < n - 1; ++bit) {
                if (mask & (1 << bit)) {
                    cur |= sum;
                    sum = y[bit + 1];
                } else {
                    sum += y[bit + 1];
                }
            }
            cur |= sum;
            ans = min(ans, cur);
        }
        cout << ans;
    } else {
        for (int i = 1; i <= n; ++i) {
            cin >> y[i];
            pref[i] = pref[i - 1] + y[i];
        }
        for (int i = 1; i <= n; ++i) {
            dp[i][1][pref[i]] = true; 
        }
        for (int k = 2; k <= b; ++k) {
            for (int i = 1; i <= n; ++i) {
                for (int j = i - 1; j >= 1; --j) {
                    for (int x = 0; x < Y; ++x) {
                        if (dp[j][k - 1][x]) {
                            dp[i][k][x | (pref[i] - pref[j])] = true;
                        }
                    }
                }
            }
        }
        ll ans = inf;
        for (int k = a; k <= b; ++k) {
            for (int x = 0; x < Y; ++x) {
                if (dp[n][k][x]) {
                    ans = min(ans, (ll)x);
                }
            }
        }
        cout << ans;
    }
}
#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...