#include <bits/stdc++.h> #include <math.h> #include <chrono> using namespace std; //#pragma GCC optimize("-O3") #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0); #define REP(i, n) for (int i = 0; i < (n); ++i) #define FWD(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define all(c) (c).begin(), (c).end() #define what(x) cerr << #x << " is " << x << endl; #define int long long #define highest(x) (63 - __builtin_clzll(x)) const int MAXN = 203; const int inf = 1e17; const int emp = 7e18 + rand(); int S[MAXN][MAXN]; int M[MAXN][MAXN][MAXN]; map<int, int> visited; int n, k; vector<int> D; int pierwszy(int val) { return (1ll << (highest(val))) - 1; } int drugi(int val) { return val - (1ll << (highest(val))); } int get(int val) { //cerr << val << " " << (lower_bound(all(D), val) - D.begin()) << endl; // return 0; return lower_bound(all(D), val) - D.begin(); } int solve(int p, int q, int val) { if (p > q) return 0; if (val < 0) { cerr << "wtf? " << val << endl; exit(-1); } if (val == 0) M[p][q][get(val)] = ((p == q) ? 0 : -inf); if (M[p][q][get(val)] != emp) return M[p][q][get(val)]; int odp = -inf; for (int i = p; i <= q + 1; i++) { odp = max(odp, solve(p, i - 1, pierwszy(val)) + solve(i, q, drugi(val)) + S[i][q]); // cerr << val << " " << pierwszy(val) << " " << drugi(val) << endl; // cerr << p << " " << i << " " << q << " " << val << " " << solve(p, i - 1, pierwszy(val)) << " " << solve(i, q, drugi(val)) << " " << S[i][q] << " " << solve(p, i - 1, pierwszy(val)) + solve(i, q, drugi(val)) + S[i][q] << endl; } return M[p][q][get(val)] = odp; } void solve() { for (int p = 0; p < n; p++) for (int q = 0; q < n; q++) M[p][q][0] = ((p == q) ? 0 : -inf); for (int l = 1; l < D.size(); l++) { int val = D[l]; int pier = get(pierwszy(val)); int dru = get(drugi(val)); for (int p = 0; p < n; p++) for (int q = p; q < n; q++) { int odp = -inf; for (int i = p; i < q; i++) odp = max(odp, M[p][i][pier] + M[i + 1][q][dru] + S[i + 1][q]); odp = max(odp, M[p][q][dru] + S[p][q]); odp = max(odp, M[p][q][pier]); M[p][q][l] = odp; } } } void dfs(int x) { visited[x] = 1; D.push_back(x); if (x == 0) return; if (visited[pierwszy(x)] == 0) dfs(pierwszy(x)); if (visited[drugi(x)] == 0) dfs(drugi(x)); } void pre() { cin >> n >> k; vector<int> P(n); rep(i, n) cin >> P[i]; rep(i, n) rep(j, n) for (int k = i; k <= j; k++) S[i][j] += P[k]; dfs(k); sort(all(D)); //cerr << D.size() << endl; } void debug() { for (int d : D) cerr << d << " "; cerr << endl; /* set<int> S; for (int i = 0; i < n - 1; i++) for (int j = i; j < n; j++) for (auto P : M[i][j]) S.insert(P.st); cerr << S.size() << endl; return; for (int i = 0; i < n - 1; i++) for (int j = i; j < n; j++) { cerr << "dla " << i << " " << j << " mapa wyglada tak: " << endl; for (auto P : M[i][j]) cerr << P.st << " " << P.nd << endl; } */ } main() { _upgrade; pre(); // debug(); //exit(-1); solve(); //solve(0, n - 1, k); cout << M[0][n - 1][get(k)] << endl; //debug(); //cerr << highest(7) << " " << highest(1ll << 40) << " " << highest(0) << " " << highest(1) << endl; }
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | #include <bits/stdc++.h> #include <math.h> #include <chrono> using namespace std; //#pragma GCC optimize("-O3") #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0); #define REP(i, n) for (int i = 0; i < (n); ++i) #define FWD(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define all(c) (c).begin(), (c).end() #define what(x) cerr << #x << " is " << x << endl; #define int long long #define highest(x) (63 - __builtin_clzll(x)) const int MAXN = 203; const int inf = 1e17; const int emp = 7e18 + rand(); int S[MAXN][MAXN]; int M[MAXN][MAXN][MAXN]; map<int, int> visited; int n, k; vector<int> D; int pierwszy(int val) { return (1ll << (highest(val))) - 1; } int drugi(int val) { return val - (1ll << (highest(val))); } int get(int val) { //cerr << val << " " << (lower_bound(all(D), val) - D.begin()) << endl; // return 0; return lower_bound(all(D), val) - D.begin(); } int solve(int p, int q, int val) { if (p > q) return 0; if (val < 0) { cerr << "wtf? " << val << endl; exit(-1); } if (val == 0) M[p][q][get(val)] = ((p == q) ? 0 : -inf); if (M[p][q][get(val)] != emp) return M[p][q][get(val)]; int odp = -inf; for (int i = p; i <= q + 1; i++) { odp = max(odp, solve(p, i - 1, pierwszy(val)) + solve(i, q, drugi(val)) + S[i][q]); // cerr << val << " " << pierwszy(val) << " " << drugi(val) << endl; // cerr << p << " " << i << " " << q << " " << val << " " << solve(p, i - 1, pierwszy(val)) << " " << solve(i, q, drugi(val)) << " " << S[i][q] << " " << solve(p, i - 1, pierwszy(val)) + solve(i, q, drugi(val)) + S[i][q] << endl; } return M[p][q][get(val)] = odp; } void solve() { for (int p = 0; p < n; p++) for (int q = 0; q < n; q++) M[p][q][0] = ((p == q) ? 0 : -inf); for (int l = 1; l < D.size(); l++) { int val = D[l]; int pier = get(pierwszy(val)); int dru = get(drugi(val)); for (int p = 0; p < n; p++) for (int q = p; q < n; q++) { int odp = -inf; for (int i = p; i < q; i++) odp = max(odp, M[p][i][pier] + M[i + 1][q][dru] + S[i + 1][q]); odp = max(odp, M[p][q][dru] + S[p][q]); odp = max(odp, M[p][q][pier]); M[p][q][l] = odp; } } } void dfs(int x) { visited[x] = 1; D.push_back(x); if (x == 0) return; if (visited[pierwszy(x)] == 0) dfs(pierwszy(x)); if (visited[drugi(x)] == 0) dfs(drugi(x)); } void pre() { cin >> n >> k; vector<int> P(n); rep(i, n) cin >> P[i]; rep(i, n) rep(j, n) for (int k = i; k <= j; k++) S[i][j] += P[k]; dfs(k); sort(all(D)); //cerr << D.size() << endl; } void debug() { for (int d : D) cerr << d << " "; cerr << endl; /* set<int> S; for (int i = 0; i < n - 1; i++) for (int j = i; j < n; j++) for (auto P : M[i][j]) S.insert(P.st); cerr << S.size() << endl; return; for (int i = 0; i < n - 1; i++) for (int j = i; j < n; j++) { cerr << "dla " << i << " " << j << " mapa wyglada tak: " << endl; for (auto P : M[i][j]) cerr << P.st << " " << P.nd << endl; } */ } main() { _upgrade; pre(); // debug(); //exit(-1); solve(); //solve(0, n - 1, k); cout << M[0][n - 1][get(k)] << endl; //debug(); //cerr << highest(7) << " " << highest(1ll << 40) << " " << highest(0) << " " << highest(1) << endl; } |