#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; typedef pair <LL,LL> lp; #define pb push_back const int INF = 2147483647; const int N = 205; LL m, tab[N], sum[N][N], T[N][N][70], Q[N][N][70], act, rest, lastRest; int n, i, j, d, u, l, k, lastK; int main() { scanf("%d %lld", &n, &m); d = 0; while ((1LL << d) <= m) d++; for (i=1;i<=n;i++) scanf("%lld", &tab[i]); for (i=1;i<=n;i++) { sum[i][i] = tab[i]; for (j=i+1;j<=n;j++) sum[i][j] = sum[i][j - 1] + tab[j]; } for (i=1;i<=n;i++) T[i][i][0] = Q[i][i][0] = 0; k = 1; rest = 1; for (j=1;j<=d;j++) { lastK = k; k = min(k * 2, n); lastRest = rest; rest = (m + 1) % (1LL << j); if (rest == 0) rest = (1LL << j); for (i=1;i<=n;i++) { for (l=i;l<=min(n, i + k - 1);l++) { T[i][l][j] = -(1LL<<62); for (u=0;u <= l - i + 1;u++) if (u <= lastK && l - i + 1 - u <= lastK) { act = 0; if (u != 0) act += T[i][i + u - 1][j - 1]; if (u != l - i + 1) { act += T[i + u][l][j - 1]; act += sum[i + u][l]; } T[i][l][j] = max(T[i][l][j], act); } if (rest == (1LL << j)) { Q[i][l][j] = T[i][l][j]; continue; } if (rest <= (1LL << (j - 1))) { Q[i][l][j] = Q[i][l][j - 1]; continue; } if (l - i + 1 > rest) continue; Q[i][l][j] = -(1LL<<62); for (u=0;u <= l - i + 1;u++) if (u <= lastK && l - i + 1 - u <= lastRest) { act = 0; if (u != 0) act += T[i][i + u - 1][j - 1]; if (u != l - i + 1) { act += Q[i + u][l][j - 1]; act += sum[i + u][l]; } Q[i][l][j] = max(Q[i][l][j], act); } //printf("%d %d %d %lld\n", i, l, j, Q[i][l][j]); } } } printf("%lld\n", Q[1][n][d]); 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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; typedef pair <LL,LL> lp; #define pb push_back const int INF = 2147483647; const int N = 205; LL m, tab[N], sum[N][N], T[N][N][70], Q[N][N][70], act, rest, lastRest; int n, i, j, d, u, l, k, lastK; int main() { scanf("%d %lld", &n, &m); d = 0; while ((1LL << d) <= m) d++; for (i=1;i<=n;i++) scanf("%lld", &tab[i]); for (i=1;i<=n;i++) { sum[i][i] = tab[i]; for (j=i+1;j<=n;j++) sum[i][j] = sum[i][j - 1] + tab[j]; } for (i=1;i<=n;i++) T[i][i][0] = Q[i][i][0] = 0; k = 1; rest = 1; for (j=1;j<=d;j++) { lastK = k; k = min(k * 2, n); lastRest = rest; rest = (m + 1) % (1LL << j); if (rest == 0) rest = (1LL << j); for (i=1;i<=n;i++) { for (l=i;l<=min(n, i + k - 1);l++) { T[i][l][j] = -(1LL<<62); for (u=0;u <= l - i + 1;u++) if (u <= lastK && l - i + 1 - u <= lastK) { act = 0; if (u != 0) act += T[i][i + u - 1][j - 1]; if (u != l - i + 1) { act += T[i + u][l][j - 1]; act += sum[i + u][l]; } T[i][l][j] = max(T[i][l][j], act); } if (rest == (1LL << j)) { Q[i][l][j] = T[i][l][j]; continue; } if (rest <= (1LL << (j - 1))) { Q[i][l][j] = Q[i][l][j - 1]; continue; } if (l - i + 1 > rest) continue; Q[i][l][j] = -(1LL<<62); for (u=0;u <= l - i + 1;u++) if (u <= lastK && l - i + 1 - u <= lastRest) { act = 0; if (u != 0) act += T[i][i + u - 1][j - 1]; if (u != l - i + 1) { act += Q[i + u][l][j - 1]; act += sum[i + u][l]; } Q[i][l][j] = max(Q[i][l][j], act); } //printf("%d %d %d %lld\n", i, l, j, Q[i][l][j]); } } } printf("%lld\n", Q[1][n][d]); return 0; } |