#include <bits/stdc++.h> using namespace std; int main(){ #define int long long ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector <int> a(n), p(n+1); for(int i = 0; i < n; ++i){ cin >> a[i]; p[i+1] = a[i]; } for(int i = 1; i <= n; ++i)p[i] += p[i-1]; //61,n,n //an*n,nb,c auto f = [&](int g, int b, int c){ int x = c + n*b + n*n*g; if(b > c)return 0LL; if(x < 0)return 0LL; return x; }; vector <int> dpp(61*n*n, -1e18),dpm; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ dpp[f(0,i,j)] = 0; } /*for(int k = 0; k < 61; ++k){ dpp[f(k,i,i-1)] = 0; }*/ } //dpp[0] = 0; dpm = dpp; for(int j = 1; j < 61; ++j){ int k = (1LL << j); int ko1 = (1LL << (j-1)); int km = ko1&m; int ko1m = (ko1 - 1)&m; for(int i1 = 0; i1 < n; ++i1){ for(int i2 = i1+1; i2 <= n; ++i2){ if(!km){ dpm[f(j,i1,i2-1)] = dpm[f(j-1,i1,i2-1)]; } int ile = i2-i1; //int suma = p[i2] - p[i1]; if(ile > k)continue; for(int i3 = i1; i3 <= i2; ++i3){//zakres max bit-u int ile1 = i3-i1; //int suma1 = p[i3] - p[i1]; int ile2 = i2-i3; int suma2 = p[i2] - p[i3]; if(ile1 <= ko1 && ile2 <= ko1){ dpp[f(j,i1,i2-1)] = max(dpp[f(j,i1,i2-1)], dpp[f(j-1,i1,i3-1)] + dpp[f(j-1,i3,i2-1)] + suma2); } if(km && ile1 <= ko1 && ile2 <= ko1m + 1){ dpm[f(j,i1,i2-1)] = max(dpm[f(j,i1,i2-1)], dpp[f(j-1,i1,i3-1)] + dpm[f(j-1,i3,i2-1)] + suma2); } } } } } cout << dpm[f(60,0,n-1)] << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; int main(){ #define int long long ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector <int> a(n), p(n+1); for(int i = 0; i < n; ++i){ cin >> a[i]; p[i+1] = a[i]; } for(int i = 1; i <= n; ++i)p[i] += p[i-1]; //61,n,n //an*n,nb,c auto f = [&](int g, int b, int c){ int x = c + n*b + n*n*g; if(b > c)return 0LL; if(x < 0)return 0LL; return x; }; vector <int> dpp(61*n*n, -1e18),dpm; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ dpp[f(0,i,j)] = 0; } /*for(int k = 0; k < 61; ++k){ dpp[f(k,i,i-1)] = 0; }*/ } //dpp[0] = 0; dpm = dpp; for(int j = 1; j < 61; ++j){ int k = (1LL << j); int ko1 = (1LL << (j-1)); int km = ko1&m; int ko1m = (ko1 - 1)&m; for(int i1 = 0; i1 < n; ++i1){ for(int i2 = i1+1; i2 <= n; ++i2){ if(!km){ dpm[f(j,i1,i2-1)] = dpm[f(j-1,i1,i2-1)]; } int ile = i2-i1; //int suma = p[i2] - p[i1]; if(ile > k)continue; for(int i3 = i1; i3 <= i2; ++i3){//zakres max bit-u int ile1 = i3-i1; //int suma1 = p[i3] - p[i1]; int ile2 = i2-i3; int suma2 = p[i2] - p[i3]; if(ile1 <= ko1 && ile2 <= ko1){ dpp[f(j,i1,i2-1)] = max(dpp[f(j,i1,i2-1)], dpp[f(j-1,i1,i3-1)] + dpp[f(j-1,i3,i2-1)] + suma2); } if(km && ile1 <= ko1 && ile2 <= ko1m + 1){ dpm[f(j,i1,i2-1)] = max(dpm[f(j,i1,i2-1)], dpp[f(j-1,i1,i3-1)] + dpm[f(j-1,i3,i2-1)] + suma2); } } } } } cout << dpm[f(60,0,n-1)] << '\n'; } |