Submission #744199

#TimeUsernameProblemLanguageResultExecution timeMemory
744199viwlesxqBali Sculptures (APIO15_sculpture)C++17
100 / 100
779 ms20300 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef string str; const ll inf = 1e18; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, a, b; cin >> n >> a >> b; ll y[n + 1]; for (int i = 0; i < n; ++i) { cin >> y[i]; } if (n <= 20) { ll ans = inf; 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 if (*max_element(y, y + n) <= 20) { ll pref[n + 1]; bool dp[n + 1][n + 1][2001]; memset(dp, false, sizeof(dp)); pref[0] = 0; for (int i = 1; i <= n; ++i) { pref[i] = pref[i - 1] + y[i - 1]; } 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 < 2001; ++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 < 2001; ++x) { if (dp[n][k][x]) { ans = min(ans, (ll)x); } } } cout << ans; } else if (n <= 100) { ll mask = 0; for (int bit = 60; bit >= 0; --bit) { bool can = false; mask |= 1ll << bit; vector <vector <bool>> dp(n + 1, vector <bool> (n + 1, false)); dp[0][0] = true; for (int k = 0; k <= b; ++k) { for (int i = 0; i < n; ++i) { ll sum = 0; if (dp[k][i]) { for (int j = i; j < n; ++j) { sum += y[j]; if ((sum & mask) == 0) { dp[k + 1][j + 1] = true; } } } } } for (int k = a; k <= b; ++k) { if (dp[k][n]) { can = true; } } if (!can) { mask ^= 1ll << bit; } } cout << (1ll << 61) - 1 - mask; } else { ll mask = 0; for (int bit = 60; bit >= 0; --bit) { mask |= 1ll << bit; vector <int> dp(n + 1, 100000); dp[0] = 0; for (int i = 0; i < n; ++i) { ll sum = 0; if (dp[i] < 100000) { for (int j = i; j < n; ++j) { sum += y[j]; if ((sum & mask) == 0) { dp[j + 1] = min(dp[j + 1], dp[i] + 1); } } } } if (dp[n] > b) { mask ^= 1ll << bit; } } cout << (1ll << 61) - 1 - mask; } }
#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...