#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); ++i)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
const int maxm = 30 * 1000 * 1000;
const ll inf = 4e18;
ll dp[maxm];
void solve() {
int n, m;
cin>>n>>m;
for(int i=0; i<n; ++i) {
ll x; cin>>x;
ll last = (i > 0 ? dp [i-1] : 0);
for(int j=i; j-1+(n-i)<=m; ++j) {
ll res = (j > i ? dp[j-1] : -inf);
res = max(res, last + x * __builtin_popcount(j));
last = dp[j];
dp[j] = res;
}
}
ll res =-inf;
for(int j=n-1; j<=m; ++j) res=max(res, dp[j]);
assert(res != -inf);
cout<<res<<"\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin>>t;
while(t--) solve();
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); ++i) #define mp make_pair #define pb push_back #define st first #define nd second typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; const int maxm = 30 * 1000 * 1000; const ll inf = 4e18; ll dp[maxm]; void solve() { int n, m; cin>>n>>m; for(int i=0; i<n; ++i) { ll x; cin>>x; ll last = (i > 0 ? dp [i-1] : 0); for(int j=i; j-1+(n-i)<=m; ++j) { ll res = (j > i ? dp[j-1] : -inf); res = max(res, last + x * __builtin_popcount(j)); last = dp[j]; dp[j] = res; } } ll res =-inf; for(int j=n-1; j<=m; ++j) res=max(res, dp[j]); assert(res != -inf); cout<<res<<"\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; //cin>>t; while(t--) solve(); return 0; } |
English