#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FOROP(i,a,b,op) for(int i=a;i<b;op) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; const ll INF = ((1LL << 62) + 44); ll T[200]; ll P[201]; ll D[62][200][201]; ll N[62][200][201]; ll DT[62][200][201]; ll NT[62][200][201]; //dokladny (trymowany) //najwyzej (trymowany) int najw1(ll m){ //kk - ile jedynek najwiecej ma liczba <= m ll p2 = 1; int kk = 0; while(p2 <= m){ p2 = 2 * p2 + 1; kk++; } return kk; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; ll m; cin >> n >> m; FOR(i,0,n) cin >> T[i]; if (m == 0){ cout << 0 << endl; return 0; } int kk = najw1(m); if (n == 1){ if (T[0] < 0){ cout << 0 << endl; return 0; } cout << T[0] * kk << endl; return 0; } P[0] = 0; FOR(i,0,n) P[i + 1] = P[i] + T[i]; FOR(i,0,n){ D[0][i][i] = N[0][i][i] = DT[0][i][i] = NT[0][i][i] = 0; D[0][i][i+1] = N[0][i][i+1] = DT[0][i][i+1] = NT[0][i][i+1] = 0; FOR(j,i+2,n+1){ D[0][i][j] = N[0][i][j] = DT[0][i][j] = NT[0][i][j] = -INF; } } FOR(k,1,kk+1){ FOR(i,0,n){ D[k][i][i] = 0; FOR(j,i+1,n+1) D[k][i][j] = (N[k - 1][i][j] == -INF ? -INF : N[k - 1][i][j] + P[j] - P[i]); } FOR(i,0,n){ N[k][i][i] = 0; FOR(j,i+1,n+1){ ll ans = max(N[k - 1][i][j], D[k][i][j]); FOR(x,i+1,j){ if (N[k - 1][i][x] != -INF && D[k][x][j] != -INF){ ans = max(ans, N[k - 1][i][x] + D[k][x][j]); } } N[k][i][j] = ans; } } } ll bc = 1; FOR(k,1,kk+2){ FOR(i,0,n){ DT[k][i][i] = 0; if ((m & bc) != 0) FOR(j,i+1,n+1) DT[k][i][j] = (NT[k - 1][i][j] == -INF ? -INF : NT[k - 1][i][j] + P[j] - P[i]); else FOR(j,i+1,n+1) DT[k][i][j] = -INF; } FOR(i,0,n){ NT[k][i][i] = 0; if ((m & bc) != 0){ FOR(j,i+1,n+1){ ll ans = max(N[k - 1][i][j], DT[k][i][j]); FOR(x,i+1,j){ if (N[k - 1][i][x] != -INF && DT[k][x][j] != -INF){ ans = max(ans, N[k - 1][i][x] + DT[k][x][j]); } } NT[k][i][j] = ans; } } else { FOR(j,i+1,n+1){ NT[k][i][j] = NT[k - 1][i][j]; } } } bc *= 2; } cout << NT[kk+1][0][n] << endl; /* FOR(k,0,kk+1){ cout << "BITY: " << k << endl; FOR(i,0,n) FOR(j,i,n+1) cout << i << " " << j << " -> N=" << N[k][i][j] << endl; } FOR(k,0,kk+1){ cout << "BITY: " << k << endl; FOR(i,0,n) FOR(j,i,n+1) cout << i << " " << j << " -> D=" << D[k][i][j] << 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 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FOROP(i,a,b,op) for(int i=a;i<b;op) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; const ll INF = ((1LL << 62) + 44); ll T[200]; ll P[201]; ll D[62][200][201]; ll N[62][200][201]; ll DT[62][200][201]; ll NT[62][200][201]; //dokladny (trymowany) //najwyzej (trymowany) int najw1(ll m){ //kk - ile jedynek najwiecej ma liczba <= m ll p2 = 1; int kk = 0; while(p2 <= m){ p2 = 2 * p2 + 1; kk++; } return kk; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; ll m; cin >> n >> m; FOR(i,0,n) cin >> T[i]; if (m == 0){ cout << 0 << endl; return 0; } int kk = najw1(m); if (n == 1){ if (T[0] < 0){ cout << 0 << endl; return 0; } cout << T[0] * kk << endl; return 0; } P[0] = 0; FOR(i,0,n) P[i + 1] = P[i] + T[i]; FOR(i,0,n){ D[0][i][i] = N[0][i][i] = DT[0][i][i] = NT[0][i][i] = 0; D[0][i][i+1] = N[0][i][i+1] = DT[0][i][i+1] = NT[0][i][i+1] = 0; FOR(j,i+2,n+1){ D[0][i][j] = N[0][i][j] = DT[0][i][j] = NT[0][i][j] = -INF; } } FOR(k,1,kk+1){ FOR(i,0,n){ D[k][i][i] = 0; FOR(j,i+1,n+1) D[k][i][j] = (N[k - 1][i][j] == -INF ? -INF : N[k - 1][i][j] + P[j] - P[i]); } FOR(i,0,n){ N[k][i][i] = 0; FOR(j,i+1,n+1){ ll ans = max(N[k - 1][i][j], D[k][i][j]); FOR(x,i+1,j){ if (N[k - 1][i][x] != -INF && D[k][x][j] != -INF){ ans = max(ans, N[k - 1][i][x] + D[k][x][j]); } } N[k][i][j] = ans; } } } ll bc = 1; FOR(k,1,kk+2){ FOR(i,0,n){ DT[k][i][i] = 0; if ((m & bc) != 0) FOR(j,i+1,n+1) DT[k][i][j] = (NT[k - 1][i][j] == -INF ? -INF : NT[k - 1][i][j] + P[j] - P[i]); else FOR(j,i+1,n+1) DT[k][i][j] = -INF; } FOR(i,0,n){ NT[k][i][i] = 0; if ((m & bc) != 0){ FOR(j,i+1,n+1){ ll ans = max(N[k - 1][i][j], DT[k][i][j]); FOR(x,i+1,j){ if (N[k - 1][i][x] != -INF && DT[k][x][j] != -INF){ ans = max(ans, N[k - 1][i][x] + DT[k][x][j]); } } NT[k][i][j] = ans; } } else { FOR(j,i+1,n+1){ NT[k][i][j] = NT[k - 1][i][j]; } } } bc *= 2; } cout << NT[kk+1][0][n] << endl; /* FOR(k,0,kk+1){ cout << "BITY: " << k << endl; FOR(i,0,n) FOR(j,i,n+1) cout << i << " " << j << " -> N=" << N[k][i][j] << endl; } FOR(k,0,kk+1){ cout << "BITY: " << k << endl; FOR(i,0,n) FOR(j,i,n+1) cout << i << " " << j << " -> D=" << D[k][i][j] << endl; }*/ } |