#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <stack> #include <bitset> #include <unordered_set> #include <unordered_map> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; typedef short int sint; #define FOR(x, b, e) for(int x=(b); x<=(e); ++x) #define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define ALL(c) c.begin(),c.end() #define SIZE(x) ((int)((x).size())) #define PB push_back #define ST first #define ND second #define DEBUG 0 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }} #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); const ll inf = 3e18; const int N = 200; const int K = 60; const int M = 2; ll tab[M][M][K][N][N]; bool cnt[M][M][K][N][N]; int mRozklad[K]; int n; ll m; ll a[N], s[N]; inline ll sum(int x, int y) { ll res = s[y]; if (x > 0) { res -= s[x - 1]; } return res; } ll f(bool isMax, bool isNonZero, int pos, int x, int y) { if (DEBUG)printf("\tf(%d, %d, %d, %d, %d)\n", isMax, isNonZero, pos, x, y); if (pos == K) { if (x == y && (isNonZero || x == 0)) { return 0; } else { return -inf; } // if (DEBUG)printf("6f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); // return res; } ll &res = tab[isMax][isNonZero][pos][x][y]; if (cnt[isMax][isNonZero][pos][x][y]) { if (DEBUG)printf("7f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); return tab[isMax][isNonZero][pos][x][y]; } cnt[isMax][isNonZero][pos][x][y] = true; int d = (y - x + 1); int bitZost = K - pos; ll dl = (1LL << bitZost); if (DEBUG)printf("d: %d, dl: %lld\n", d, dl); if (isMax && mRozklad[pos] == 0) { res = f(true, isNonZero, pos + 1, x, y); if (DEBUG)printf("4f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); return res; } else if (d > dl) { res = -inf; if (DEBUG)printf("3f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); return res; } else if (isMax && mRozklad[pos] == 0) { res = f(true, isNonZero, pos + 1, x, y); if (DEBUG)printf("4f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); return res; } else { // same 0 int bestD = 0; res = -inf; res = max(res, f(false, isNonZero, pos + 1, x, y)); FOR(i, 1, d - 1) { // i <- oznacza ile zer // d - i ll temp = sum(x + i, y) + f(isMax, true, pos + 1, x + i, y) + f(false, isNonZero, pos + 1, x, x + i - 1); if (temp > res) { res = temp; bestD = d - i; } } // same 1 ll temp = sum(x, y) + f(isMax, true, pos + 1, x, y); if (temp > res) { res = temp; bestD = d; } if (DEBUG)printf("5f(%d, %d, %d, %d, %d) - %lld, with %d ones\n", isMax, isNonZero, pos, x, y, res, bestD); return res; } } int main() { cin >> n >> m; REP(i, n) { cin >> a[i]; } s[0] = a[0]; FOR(i, 1, n - 1) { s[i] = s[i - 1] + a[i]; } int fPos = -1; REP(i, K) { int wyk = K - i - 1; if (m & (1LL << wyk)) { if (fPos == -1) fPos = i; mRozklad[i] = 1; } else { mRozklad[i] = 0; } } ll res = f(true, false, fPos, 0, n - 1); cout << res << endl; 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <stack> #include <bitset> #include <unordered_set> #include <unordered_map> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; typedef short int sint; #define FOR(x, b, e) for(int x=(b); x<=(e); ++x) #define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define ALL(c) c.begin(),c.end() #define SIZE(x) ((int)((x).size())) #define PB push_back #define ST first #define ND second #define DEBUG 0 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }} #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); const ll inf = 3e18; const int N = 200; const int K = 60; const int M = 2; ll tab[M][M][K][N][N]; bool cnt[M][M][K][N][N]; int mRozklad[K]; int n; ll m; ll a[N], s[N]; inline ll sum(int x, int y) { ll res = s[y]; if (x > 0) { res -= s[x - 1]; } return res; } ll f(bool isMax, bool isNonZero, int pos, int x, int y) { if (DEBUG)printf("\tf(%d, %d, %d, %d, %d)\n", isMax, isNonZero, pos, x, y); if (pos == K) { if (x == y && (isNonZero || x == 0)) { return 0; } else { return -inf; } // if (DEBUG)printf("6f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); // return res; } ll &res = tab[isMax][isNonZero][pos][x][y]; if (cnt[isMax][isNonZero][pos][x][y]) { if (DEBUG)printf("7f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); return tab[isMax][isNonZero][pos][x][y]; } cnt[isMax][isNonZero][pos][x][y] = true; int d = (y - x + 1); int bitZost = K - pos; ll dl = (1LL << bitZost); if (DEBUG)printf("d: %d, dl: %lld\n", d, dl); if (isMax && mRozklad[pos] == 0) { res = f(true, isNonZero, pos + 1, x, y); if (DEBUG)printf("4f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); return res; } else if (d > dl) { res = -inf; if (DEBUG)printf("3f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); return res; } else if (isMax && mRozklad[pos] == 0) { res = f(true, isNonZero, pos + 1, x, y); if (DEBUG)printf("4f(%d, %d, %d, %d, %d) - %lld\n", isMax, isNonZero, pos, x, y, res); return res; } else { // same 0 int bestD = 0; res = -inf; res = max(res, f(false, isNonZero, pos + 1, x, y)); FOR(i, 1, d - 1) { // i <- oznacza ile zer // d - i ll temp = sum(x + i, y) + f(isMax, true, pos + 1, x + i, y) + f(false, isNonZero, pos + 1, x, x + i - 1); if (temp > res) { res = temp; bestD = d - i; } } // same 1 ll temp = sum(x, y) + f(isMax, true, pos + 1, x, y); if (temp > res) { res = temp; bestD = d; } if (DEBUG)printf("5f(%d, %d, %d, %d, %d) - %lld, with %d ones\n", isMax, isNonZero, pos, x, y, res, bestD); return res; } } int main() { cin >> n >> m; REP(i, n) { cin >> a[i]; } s[0] = a[0]; FOR(i, 1, n - 1) { s[i] = s[i - 1] + a[i]; } int fPos = -1; REP(i, K) { int wyk = K - i - 1; if (m & (1LL << wyk)) { if (fPos == -1) fPos = i; mRozklad[i] = 1; } else { mRozklad[i] = 0; } } ll res = f(true, false, fPos, 0, n - 1); cout << res << endl; return 0; } |