#include <cstdio> #include <algorithm> #define REP(a, n) for (int a = 0; a < (n); ++a) #define FOR(a, b, c) for (int a = (b); a <= (c); ++a) // 123456789012345678 #define INF (1300000000000000000LL) typedef long long LL; using namespace std; ////////////////////// LL tab[200], suma[201], best[201][201][64], last[201][64]; int N; LL M; inline int wszystkie(int b) { return min(N + 1LL, 1LL << b); } inline int to_last_bits(int b) { return min(N + 1LL, (M & ((1LL << b) - 1)) + 1); } int main() { scanf("%d%lld", &N, &M); REP(a, N) { scanf("%lld", &tab[a]); suma[a + 1] = suma[a] + tab[a]; } FOR(b, 1, 61) REP(i, N) { FOR(k, i + 1, min(N, i + wszystkie(b))) { best[i][k][b] = -INF; int beg = max(k - wszystkie(b - 1), i); int end = min(i + wszystkie(b - 1), k); FOR(j, beg, end) best[i][k][b] = max(best[i][k][b], best[i][j][b - 1] + best[j][k][b - 1] + suma[k] - suma[j]); } if (M & (1LL << (b - 1))) { last[i][b] = -INF; int beg = max(N - to_last_bits(b - 1), i); int end = min(i + wszystkie(b - 1), N); FOR(j, beg, end) last[i][b] = max(last[i][b], best[i][j][b - 1] + last[j][b - 1] + suma[N] - suma[j]); } else last[i][b] = last[i][b - 1]; } printf("%lld\n", last[0][61]); }
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 | #include <cstdio> #include <algorithm> #define REP(a, n) for (int a = 0; a < (n); ++a) #define FOR(a, b, c) for (int a = (b); a <= (c); ++a) // 123456789012345678 #define INF (1300000000000000000LL) typedef long long LL; using namespace std; ////////////////////// LL tab[200], suma[201], best[201][201][64], last[201][64]; int N; LL M; inline int wszystkie(int b) { return min(N + 1LL, 1LL << b); } inline int to_last_bits(int b) { return min(N + 1LL, (M & ((1LL << b) - 1)) + 1); } int main() { scanf("%d%lld", &N, &M); REP(a, N) { scanf("%lld", &tab[a]); suma[a + 1] = suma[a] + tab[a]; } FOR(b, 1, 61) REP(i, N) { FOR(k, i + 1, min(N, i + wszystkie(b))) { best[i][k][b] = -INF; int beg = max(k - wszystkie(b - 1), i); int end = min(i + wszystkie(b - 1), k); FOR(j, beg, end) best[i][k][b] = max(best[i][k][b], best[i][j][b - 1] + best[j][k][b - 1] + suma[k] - suma[j]); } if (M & (1LL << (b - 1))) { last[i][b] = -INF; int beg = max(N - to_last_bits(b - 1), i); int end = min(i + wszystkie(b - 1), N); FOR(j, beg, end) last[i][b] = max(last[i][b], best[i][j][b - 1] + last[j][b - 1] + suma[N] - suma[j]); } else last[i][b] = last[i][b - 1]; } printf("%lld\n", last[0][61]); } |