#include <bits/stdc++.h> using namespace std; int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); int n; long long m; cin >> n >> m; vector<long long> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<long long> s(n + 1); for (int i = 0; i < n; ++i) { s[i + 1] = s[i] + a[i]; } vector<vector<long long>> foo(n, vector<long long>(n + 1, -(1ll << 60))); vector<vector<long long>> bar(n, vector<long long>(n + 1, -(1ll << 60))); for (int i = 0; i < n; ++i) { foo[i][i + 1] = bar[i][i + 1] = 0; } for (int d = 0; d < 60; ++d) { vector<vector<long long>> new_foo(n, vector<long long>(n + 1, -(1ll << 60))); vector<vector<long long>> new_bar(n, vector<long long>(n + 1, -(1ll << 60))); for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { for (int k = i; k <= j; ++k) { new_foo[i][j] = max(new_foo[i][j], (i < k ? foo[i][k] : 0) + (k < j ? foo[k][j] : 0) + s[j] - s[k]); } } } for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { if (m >> d & 1) { for (int k = i; k <= j; ++k) { new_bar[i][j] = max(new_bar[i][j], (i < k ? foo[i][k] : 0) + (k < j ? bar[k][j] : 0) + s[j] - s[k]); } } else { new_bar[i][j] = bar[i][j]; } } } swap(foo, new_foo); swap(bar, new_bar); } cout << bar[0][n] << "\n"; 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 | #include <bits/stdc++.h> using namespace std; int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); int n; long long m; cin >> n >> m; vector<long long> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<long long> s(n + 1); for (int i = 0; i < n; ++i) { s[i + 1] = s[i] + a[i]; } vector<vector<long long>> foo(n, vector<long long>(n + 1, -(1ll << 60))); vector<vector<long long>> bar(n, vector<long long>(n + 1, -(1ll << 60))); for (int i = 0; i < n; ++i) { foo[i][i + 1] = bar[i][i + 1] = 0; } for (int d = 0; d < 60; ++d) { vector<vector<long long>> new_foo(n, vector<long long>(n + 1, -(1ll << 60))); vector<vector<long long>> new_bar(n, vector<long long>(n + 1, -(1ll << 60))); for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { for (int k = i; k <= j; ++k) { new_foo[i][j] = max(new_foo[i][j], (i < k ? foo[i][k] : 0) + (k < j ? foo[k][j] : 0) + s[j] - s[k]); } } } for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { if (m >> d & 1) { for (int k = i; k <= j; ++k) { new_bar[i][j] = max(new_bar[i][j], (i < k ? foo[i][k] : 0) + (k < j ? bar[k][j] : 0) + s[j] - s[k]); } } else { new_bar[i][j] = bar[i][j]; } } } swap(foo, new_foo); swap(bar, new_bar); } cout << bar[0][n] << "\n"; return 0; } |