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 #include 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_["<