#include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr ll INF = 2e18; ll dp[205][205][65][2]; int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(18); int n; ll masks[2] = {0, INT64_MAX}; cin >> n >> masks[0]; vector<ll> elems(n); vector<ll> prefs = {0}; each(e, elems) { cin >> e; prefs.pb(prefs.back()+e); } rep(begin, 0, n) rep(bits, 0, 62) rep(type, 0, 2) { ll mask = masks[type] & ((1LL<<bits)-1); int lit = 63 - __builtin_clzll(mask+1); dp[begin][begin+1][bits][type] = (elems[begin] > 0 ? elems[begin] * lit : 0); } rep(len, 2, n+1) rep(begin, 0, n-len+1) { int end = begin+len; rep(type, 0, 2) { dp[begin][end][0][type] = -INF; } rep(bits, 1, 62) rep(type, 0, 2) { ll best = -INF; if ((masks[type] >> (bits-1)) & 1) { rep(mid, begin, end+1) { ll alt = dp[begin][mid][bits-1][1] + dp[mid][end][bits-1][type]; alt += prefs[end] - prefs[mid]; best = max(best, alt); } } else { best = dp[begin][end][bits-1][type]; } dp[begin][end][bits][type] = best; } } ll ans = dp[0][n][61][0]; cout << ans << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr ll INF = 2e18; ll dp[205][205][65][2]; int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(18); int n; ll masks[2] = {0, INT64_MAX}; cin >> n >> masks[0]; vector<ll> elems(n); vector<ll> prefs = {0}; each(e, elems) { cin >> e; prefs.pb(prefs.back()+e); } rep(begin, 0, n) rep(bits, 0, 62) rep(type, 0, 2) { ll mask = masks[type] & ((1LL<<bits)-1); int lit = 63 - __builtin_clzll(mask+1); dp[begin][begin+1][bits][type] = (elems[begin] > 0 ? elems[begin] * lit : 0); } rep(len, 2, n+1) rep(begin, 0, n-len+1) { int end = begin+len; rep(type, 0, 2) { dp[begin][end][0][type] = -INF; } rep(bits, 1, 62) rep(type, 0, 2) { ll best = -INF; if ((masks[type] >> (bits-1)) & 1) { rep(mid, begin, end+1) { ll alt = dp[begin][mid][bits-1][1] + dp[mid][end][bits-1][type]; alt += prefs[end] - prefs[mid]; best = max(best, alt); } } else { best = dp[begin][end][bits-1][type]; } dp[begin][end][bits][type] = best; } } ll ans = dp[0][n][61][0]; cout << ans << endl; return 0; } |