#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, n) for(auto &i : n) #define SZ(x) (int)(x).size() #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; const ll INF = 3ll*1000000000ll*1000000000ll; int n; ll A[205], dp[205][205][130]; PII lp[130]; int fre; std::unordered_map<ll, int> map; int xdrec(ll a){ if(map.count(a)) return map[a]; map[a] = fre++; int xd = 63-__builtin_clzll(a); ll big = 1ll<<xd; lp[map[a]].X = xdrec(big-1); lp[map[a]].Y = xdrec(a-big); return map[a]; } ll rec(int l, int r, int val){ if(val == 0) return l == r ? 0ll : -INF; if(dp[l][r][val] != INF) return dp[l][r][val]; ll best = rec(l, r, lp[val].X); ll add = 0; for(int m = r; m > l; --m){ add += A[m]; ll cand = rec(l, m-1, lp[val].X)+add+rec(m, r, lp[val].Y); best = std::max(cand, best); } add += A[l]; best = std::max(best, add+rec(l, r, lp[val].Y)); return dp[l][r][val] = best; } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); ll m; std::cin >> n >> m; FOR(i, n) std::cin >> A[i+1]; map[0] = 0; fre = 1; xdrec(m); FOR(i, n+1) FOR(j, n+1) FOR(a, fre) dp[i][j][a] = INF; std::cout << rec(1, n, map[m]); 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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, n) for(auto &i : n) #define SZ(x) (int)(x).size() #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; const ll INF = 3ll*1000000000ll*1000000000ll; int n; ll A[205], dp[205][205][130]; PII lp[130]; int fre; std::unordered_map<ll, int> map; int xdrec(ll a){ if(map.count(a)) return map[a]; map[a] = fre++; int xd = 63-__builtin_clzll(a); ll big = 1ll<<xd; lp[map[a]].X = xdrec(big-1); lp[map[a]].Y = xdrec(a-big); return map[a]; } ll rec(int l, int r, int val){ if(val == 0) return l == r ? 0ll : -INF; if(dp[l][r][val] != INF) return dp[l][r][val]; ll best = rec(l, r, lp[val].X); ll add = 0; for(int m = r; m > l; --m){ add += A[m]; ll cand = rec(l, m-1, lp[val].X)+add+rec(m, r, lp[val].Y); best = std::max(cand, best); } add += A[l]; best = std::max(best, add+rec(l, r, lp[val].Y)); return dp[l][r][val] = best; } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); ll m; std::cin >> n >> m; FOR(i, n) std::cin >> A[i+1]; map[0] = 0; fre = 1; xdrec(m); FOR(i, n+1) FOR(j, n+1) FOR(a, fre) dp[i][j][a] = INF; std::cout << rec(1, n, map[m]); return 0; } |