#include<bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(),(x).end() #define SIZE(x) (int)(x).size() #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define REP(x, n) for(int x = 0; x < (n); x++) #define FOR(x, b, e) for(int x = (b); x <= (e); x++) #define FORD(x, b, e) for(int x = (b); x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) typedef long long int LL; typedef pair<int,int> PII; typedef vector<int> VI; const int MXN = 210; const int C = 262144; const LL INF = LLONG_MAX; LL a[MXN]; int n; LL m; LL pref[MXN]; int bits(LL x) { if(x == 0) return 0; return 64 - __builtin_clzll(x); } LL sum(int pocz, int kon) { if(pocz > kon) return 0; return pref[kon] - pref[pocz - 1]; } LL DP[MXN][MXN][65][2]; void test() { scanf("%d %lld", &n, &m); FOR(i, 1, n) { scanf("%lld", &a[i]); pref[i] = a[i] + pref[i - 1]; } int bity = bits(m); FOR(k, 1, bity) FOR(i, 1, n) FOR(j, i, n) DP[i][j][k][0] = DP[i][j][k][1] = -INF; FOR(i, 1, n) { DP[i][i][1][0] = max(0LL, a[i]); if(m % 2) DP[i][i][1][1] = max(0LL, a[i]); else DP[i][i][1][1] = 0; if(i < n) { DP[i][i + 1][1][0] = a[i + 1]; if(m % 2) DP[i][i + 1][1][1] = a[i + 1]; else DP[i][i + 1][1][1] = -INF; } } FOR(k, 2, bity) FOR(len, 1, n) FOR(i, 1, n) { int j = i + len - 1; if(j > n) continue; FOR(c, 0, len) { LL t1 = DP[i][i + c - 1][k - 1][0]; LL t2 = DP[i + c][j][k - 1][0]; if(t1 != -INF && t2 != -INF) DP[i][j][k][0] = max(DP[i][j][k][0], t1 + t2 + sum(i + c, j) ); } if( (1LL << (k - 1) ) & m) { FOR(c, 0, len) { LL t1 = DP[i][i + c - 1][k - 1][0]; LL t2 = DP[i + c][j][k - 1][1]; if(t1 != -INF && t2 != -INF) DP[i][j][k][1] = max(DP[i][j][k][1], t1 + t2 + sum(i + c, j) ); } } else DP[i][j][k][1] = DP[i][j][k - 1][1]; } printf("%lld\n", DP[1][n][bity][1]); } int main() { int te = 1; while(te--) test(); 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 | #include<bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(),(x).end() #define SIZE(x) (int)(x).size() #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define REP(x, n) for(int x = 0; x < (n); x++) #define FOR(x, b, e) for(int x = (b); x <= (e); x++) #define FORD(x, b, e) for(int x = (b); x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) typedef long long int LL; typedef pair<int,int> PII; typedef vector<int> VI; const int MXN = 210; const int C = 262144; const LL INF = LLONG_MAX; LL a[MXN]; int n; LL m; LL pref[MXN]; int bits(LL x) { if(x == 0) return 0; return 64 - __builtin_clzll(x); } LL sum(int pocz, int kon) { if(pocz > kon) return 0; return pref[kon] - pref[pocz - 1]; } LL DP[MXN][MXN][65][2]; void test() { scanf("%d %lld", &n, &m); FOR(i, 1, n) { scanf("%lld", &a[i]); pref[i] = a[i] + pref[i - 1]; } int bity = bits(m); FOR(k, 1, bity) FOR(i, 1, n) FOR(j, i, n) DP[i][j][k][0] = DP[i][j][k][1] = -INF; FOR(i, 1, n) { DP[i][i][1][0] = max(0LL, a[i]); if(m % 2) DP[i][i][1][1] = max(0LL, a[i]); else DP[i][i][1][1] = 0; if(i < n) { DP[i][i + 1][1][0] = a[i + 1]; if(m % 2) DP[i][i + 1][1][1] = a[i + 1]; else DP[i][i + 1][1][1] = -INF; } } FOR(k, 2, bity) FOR(len, 1, n) FOR(i, 1, n) { int j = i + len - 1; if(j > n) continue; FOR(c, 0, len) { LL t1 = DP[i][i + c - 1][k - 1][0]; LL t2 = DP[i + c][j][k - 1][0]; if(t1 != -INF && t2 != -INF) DP[i][j][k][0] = max(DP[i][j][k][0], t1 + t2 + sum(i + c, j) ); } if( (1LL << (k - 1) ) & m) { FOR(c, 0, len) { LL t1 = DP[i][i + c - 1][k - 1][0]; LL t2 = DP[i + c][j][k - 1][1]; if(t1 != -INF && t2 != -INF) DP[i][j][k][1] = max(DP[i][j][k][1], t1 + t2 + sum(i + c, j) ); } } else DP[i][j][k][1] = DP[i][j][k - 1][1]; } printf("%lld\n", DP[1][n][bity][1]); } int main() { int te = 1; while(te--) test(); return 0; } |