#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 pb pop_back #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 int LL #define I(x) #x " = ", (x), " " #define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " " const int M = 205, M2 = 62; const int inf = 2e18L; int n, m, T[M], P[M]; int dp[M][M][M2], dp2[M][M2]; int sum(int p, int k) { return P[k] - P[p - 1]; } int32_t main() { //ios_base::sync_with_stdio(0); //cin.tie(NULL); scanf("%lld%lld", &n, &m); FOR(i, 1, n) { scanf("%lld", T + i); P[i] = T[i] + P[i - 1]; } FOR(i, 1, n) { dp[i][i][0] = 0; FOR(j, i + 1, n) { dp[i][j][0] = -inf; } } FOR(len, 1, n) { FOR(i, 1, n - len + 1) { int j = i + len - 1; FOR(k, 1, M2 - 1) { dp[i][j][k] = -inf; maxi(dp[i][j][k], dp[i][j][k - 1] + max(0LL, sum(i, j))); FOR(l, i, j - 1) { maxi(dp[i][j][k], dp[i][l][k - 1] + dp[l + 1][j][k - 1] + sum(l + 1, j)); } } } } m++; int ind = 0; while ((m>>(ind + 1))) { ind++; } FOR(i, 1, n) { dp2[i][ind] = dp[1][i][ind]; FORD(j, ind - 1, 0) { dp2[i][j] = dp2[i][j + 1]; if ((m>>j) % 2 == 0) { continue; } int ile = __builtin_popcountll(m>>(j + 1)); maxi(dp2[i][j], dp[1][i][j] + sum(1, i) * ile); FOR(k, 2, i) { maxi(dp2[i][j], dp2[k - 1][j + 1] + dp[k][i][j] + sum(k, i) * ile); } } } printf("%lld\n", dp2[n][0]); 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #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 pb pop_back #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 int LL #define I(x) #x " = ", (x), " " #define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " " const int M = 205, M2 = 62; const int inf = 2e18L; int n, m, T[M], P[M]; int dp[M][M][M2], dp2[M][M2]; int sum(int p, int k) { return P[k] - P[p - 1]; } int32_t main() { //ios_base::sync_with_stdio(0); //cin.tie(NULL); scanf("%lld%lld", &n, &m); FOR(i, 1, n) { scanf("%lld", T + i); P[i] = T[i] + P[i - 1]; } FOR(i, 1, n) { dp[i][i][0] = 0; FOR(j, i + 1, n) { dp[i][j][0] = -inf; } } FOR(len, 1, n) { FOR(i, 1, n - len + 1) { int j = i + len - 1; FOR(k, 1, M2 - 1) { dp[i][j][k] = -inf; maxi(dp[i][j][k], dp[i][j][k - 1] + max(0LL, sum(i, j))); FOR(l, i, j - 1) { maxi(dp[i][j][k], dp[i][l][k - 1] + dp[l + 1][j][k - 1] + sum(l + 1, j)); } } } } m++; int ind = 0; while ((m>>(ind + 1))) { ind++; } FOR(i, 1, n) { dp2[i][ind] = dp[1][i][ind]; FORD(j, ind - 1, 0) { dp2[i][j] = dp2[i][j + 1]; if ((m>>j) % 2 == 0) { continue; } int ile = __builtin_popcountll(m>>(j + 1)); maxi(dp2[i][j], dp[1][i][j] + sum(1, i) * ile); FOR(k, 2, i) { maxi(dp2[i][j], dp2[k - 1][j + 1] + dp[k][i][j] + sum(k, i) * ile); } } } printf("%lld\n", dp2[n][0]); return 0; } |