#include<cstdio> long long * pows; long long * pows_1; int B = 62; int n; long long * A; long long m; void prepare() { long long k = 1; pows = new long long[B]; pows_1 = new long long[B]; for (int i = 0; i < B; i++) { pows[i] = k; pows_1[i] = k - 1; k *= 2; } } int minb(long long a, long long b) { if (b == 0) { return 0; } long long prev = 0; for (int i = 0; i < B; i++) { if (pows[i] >= a && pows[i] <= b) { return 1; } if (pows[i] < a) { prev = pows[i]; } } return 1 + minb(a - prev, b - prev); } int maxb(long long a, long long b) { if (b == 0) { return 0; } for (int i = B - 1; i >= 0; i--) { if (pows_1[i] >= a && pows_1[i] <= b) { return i; } if (pows_1[i] < a) { return 1 + maxb(a - pows_1[i] - 1, b - pows_1[i] - 1); } } return 0; } void input() { scanf("%d %lld", &n, &m); A = new long long[n]; for (int i = 0; i < n; i++) { scanf("%lld", A + i); } } long long max(long long a, long long b) { if (a >= b) { return a; } return b; } long long solve(int k, long long q) { if (k == 0) { return 0; } long long best = -4000000000000000000LL; for (long long mid = q; mid >= k - 1; --mid) { long long cost = solve(k - 1, mid - 1); if (A[k - 1] > 0) { cost += (A[k - 1] * maxb(mid, q)); } if (A[k - 1] < 0) { cost += (A[k - 1] * minb(mid, q)); } best = max(best, cost); //printf("k=%d q=%lld mid=%lld cost=%lld A=%lld min=%d max=%d best=%lld\n", k, q, mid, cost, A[k-1], minb(mid, q), maxb(mid, q), best); } //printf("%d %lld %lld\n", k, q, best); return best; } int main() { prepare(); input(); printf("%lld\n", solve(n, m)); //printf("%d\n", minb(0, 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 | #include<cstdio> long long * pows; long long * pows_1; int B = 62; int n; long long * A; long long m; void prepare() { long long k = 1; pows = new long long[B]; pows_1 = new long long[B]; for (int i = 0; i < B; i++) { pows[i] = k; pows_1[i] = k - 1; k *= 2; } } int minb(long long a, long long b) { if (b == 0) { return 0; } long long prev = 0; for (int i = 0; i < B; i++) { if (pows[i] >= a && pows[i] <= b) { return 1; } if (pows[i] < a) { prev = pows[i]; } } return 1 + minb(a - prev, b - prev); } int maxb(long long a, long long b) { if (b == 0) { return 0; } for (int i = B - 1; i >= 0; i--) { if (pows_1[i] >= a && pows_1[i] <= b) { return i; } if (pows_1[i] < a) { return 1 + maxb(a - pows_1[i] - 1, b - pows_1[i] - 1); } } return 0; } void input() { scanf("%d %lld", &n, &m); A = new long long[n]; for (int i = 0; i < n; i++) { scanf("%lld", A + i); } } long long max(long long a, long long b) { if (a >= b) { return a; } return b; } long long solve(int k, long long q) { if (k == 0) { return 0; } long long best = -4000000000000000000LL; for (long long mid = q; mid >= k - 1; --mid) { long long cost = solve(k - 1, mid - 1); if (A[k - 1] > 0) { cost += (A[k - 1] * maxb(mid, q)); } if (A[k - 1] < 0) { cost += (A[k - 1] * minb(mid, q)); } best = max(best, cost); //printf("k=%d q=%lld mid=%lld cost=%lld A=%lld min=%d max=%d best=%lld\n", k, q, mid, cost, A[k-1], minb(mid, q), maxb(mid, q), best); } //printf("%d %lld %lld\n", k, q, best); return best; } int main() { prepare(); input(); printf("%lld\n", solve(n, m)); //printf("%d\n", minb(0, 0)); return 0; } |