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
#include <cstdio>
#include <algorithm>

using namespace std;

long long a[256], best[256], best2[256];

long long INF = 1LL << 62;

int main()
{
  int n;
  long long m;
  scanf("%d%lld", &n, &m);
  
  for (int i = 0; i < n; i++)
  {
    scanf("%lld", a + i);
    best[i] = -INF;
    best2[i] = -INF;
  }
  
  
  best[0] = 0;

  for (int i = 1; i < n; i++)
  {
    int b = __builtin_popcount(i);
    best2[0] = max(b * a[0], best[0]);
    for (int j = 1; j <= i; j++)
    {
      best2[j] = max(best[j], b * a[j] + best[j - 1]);
    }
    for (int j = 0; j <= i; j++) best[j] = best2[j];
  }
  

  for (int i = n; i <= m; i++)
  {
    int b = __builtin_popcount(i);
    best2[0] = max(b * a[0], best[0]);
    for (int j = 1; j < n; j++)
    {
      best2[j] = b * a[j] + best[j - 1];
    }
    for (int j = 0; j < n; j++) 
    {
      best[j] = max(best2[j], best[j]);
    }
  }
  
  printf("%lld\n", best[n - 1]);
  
  return 0;
}