#include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " // ****************************************************************************** #define bi(x) bitset<60>(x) #define bity(x) __builtin_popcountll(x) #define leading(x) (63-__builtin_clzll(x)) const int MXN = 205; const LL INF = 4e18; LL t[MXN]; LL pref[MXN]; LL sum(int a, int b) { return pref[b] - pref[a-1]; } bool done[65][2][MXN][MXN]; LL dp[65][2][MXN][MXN]; /* Rozwiazuje zadanie na przedziale [a, b] wejściowego ciągu. m oraz (ones, len) wzajemnie z siebie wynikają */ LL go(int a, int b, LL m, bool ones, int len) { if(a > b)return 0; if(a == b) { if(t[a] <= 0)return 0; return t[a] * leading(m+1); } if(m == 0)return -INF; if(done[len][ones][a][b])return dp[len][ones][a][b]; done[len][ones][a][b] = 1; LL &res = dp[len][ones][a][b] = -INF; if(!(m & (1LL << len)))return res = go(a, b, m, ones, len-1); FOR(i, a, b+1) { maxi(res, go(a, i-1, (1LL << len)-1, 1, len-1) + go(i, b, m - (1LL << len), ones, len-1) + sum(i, b)); } return res; } int main() { int n; LL m; cin >> n >> m; FOR(i, 1, n)cin >> t[i]; FOR(i, 1, n)pref[i] = pref[i-1] + t[i]; cout << go(1, n, m, 0, 60) << endl; }
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " // ****************************************************************************** #define bi(x) bitset<60>(x) #define bity(x) __builtin_popcountll(x) #define leading(x) (63-__builtin_clzll(x)) const int MXN = 205; const LL INF = 4e18; LL t[MXN]; LL pref[MXN]; LL sum(int a, int b) { return pref[b] - pref[a-1]; } bool done[65][2][MXN][MXN]; LL dp[65][2][MXN][MXN]; /* Rozwiazuje zadanie na przedziale [a, b] wejściowego ciągu. m oraz (ones, len) wzajemnie z siebie wynikają */ LL go(int a, int b, LL m, bool ones, int len) { if(a > b)return 0; if(a == b) { if(t[a] <= 0)return 0; return t[a] * leading(m+1); } if(m == 0)return -INF; if(done[len][ones][a][b])return dp[len][ones][a][b]; done[len][ones][a][b] = 1; LL &res = dp[len][ones][a][b] = -INF; if(!(m & (1LL << len)))return res = go(a, b, m, ones, len-1); FOR(i, a, b+1) { maxi(res, go(a, i-1, (1LL << len)-1, 1, len-1) + go(i, b, m - (1LL << len), ones, len-1) + sum(i, b)); } return res; } int main() { int n; LL m; cin >> n >> m; FOR(i, 1, n)cin >> t[i]; FOR(i, 1, n)pref[i] = pref[i-1] + t[i]; cout << go(1, n, m, 0, 60) << endl; } |