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
#include <iostream>
#include <vector>

using int64 = long long;
const int64 kMinInt64 = -2'000'000'000'000'000'000;

class PopAmplifier {
   public:
    PopAmplifier() {
        std::cin >> n_ >> m_;
        A_[0] = 0LL;
        ps[0] = 0;
        for (int i = 0; i < n_; i++) {
            std::cin >> A_[i];
            ps[i + 1] = ps[i] + A_[i];
        }
    }

    void compute_optimal_trees() {
        // First, compute the trees of height 1.
        for (int a = 0; a < n_; a++) {
            full_tree_[1][a][a] = 0;
            full_tree_[1][a][a + 1] = 0;

            part_tree_[1][a][a] = 0;
            part_tree_[1][a][a + 1] = 0;
        }

        int64 full_leaves = 1LL;
        int64 part_leaves = 1LL;
        int h = 2;
        for (int64 m = m_; m > 0; h++) {
            bool right = (m % 2LL) == 1LL;
            const int64 right_leaves = part_leaves;
            const int64 child_leaves = full_leaves;
            if (right) {
                part_leaves += full_leaves;
            }
            full_leaves *= 2LL;
            m /= 2LL;

            for (int64 a = 0; a < n_; a++) {
                full_tree_[h][a][a] = part_tree_[h][a][a] = 0LL;
                for (int64 b = a + 1; b <= n_ && b <= a + full_leaves; b++) {
                    full_tree_[h][a][b] = kMinInt64;
                    // [a,s) goes to the left subtree. [s,b) goes to the right.
                    for (int s = std::max(a, b - child_leaves);
                         s <= std::min(b, child_leaves + a); s++) {
                        // The right subtree has an additional 1-bit everyone
                        // needs to be multiplied by.
                        full_tree_[h][a][b] =
                            std::max(full_tree_[h][a][b],
                                     full_tree_[h - 1][a][s] +
                                         full_tree_[h - 1][s][b] + sum(s, b));
                    }
                    // std::cerr << "  - full_tree_["<<h << ","<< a << ","<< b << "] = " << full_tree_[h][a][b] << std::endl;
                }

                for (int64 b = a + 1; b <= n_ && b <= a + part_leaves; b++) {
                    if (!right) {
                        part_tree_[h][a][b] = part_tree_[h - 1][a][b];
                        continue;
                    }
                    part_tree_[h][a][b] = kMinInt64;
                    // [a,s) goes to the left subtree. [s,b) goes to the right.
                    for (int s = std::max(a, b - right_leaves);
                         s <= std::min(b, child_leaves + a); s++) {
                        // The right subtree has an additional 1-bit everyone
                        // needs to be multiplied by.
                        part_tree_[h][a][b] =
                            std::max(part_tree_[h][a][b],
                                     full_tree_[h - 1][a][s] +
                                         part_tree_[h - 1][s][b] + sum(s, b));
                    }
                    // std::cerr << "  - part_tree_["<<h << ","<< a << ","<< b << "] = " << part_tree_[h][a][b] << std::endl;
                }
            }
        }
        std::cout << part_tree_[h - 1][0][n_] << std::endl;
    }

    // Returns sum of A_ in interval [a,b).
    inline int64 sum(int a, int b) { return ps[b] - ps[a]; }

   private:
    int64 n_, m_;
    int64 A_[256];

    // ps contains prefix sums of A_.
    int64 ps[256];

    // full_tree_ contains optimum beat values for intervals of a and given full
    // tree height.
    int64 full_tree_[64][256][256];
    // part_tree_ contains optimum beat values for intervals on a tree with cut
    // off part.
    int64 part_tree_[64][256][256];
};

int main() {
    PopAmplifier A;
    A.compute_optimal_trees();
}