
 此题直接按树形dp做即可,每次从0枚举到k转移状态
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define endl '\n'
#define lson node << 1
#define rson node << 1 | 1
const int maxn = 1e5 + 5;
const int maxk = 100;
int dp[maxn][maxk],n,k,res;
int a[maxn];
int check(int node){
    if (node > n || a[node] == -1) return false;
    return true;
}
void dfs(int node){
    dp[node][1] = a[node];
    int maxx = 0;
    if (check(lson)){
        dfs(lson);
        maxx = max(maxx,dp[lson][0]);
        for (int i = 1; i <= k; ++i) {
            maxx = max(maxx,dp[lson][i]);
            dp[node][i] = max(dp[node][i],dp[lson][i - 1] + a[i]);
        }
    }
    if (check(rson)){
        dfs(rson);
        maxx = max(maxx,dp[rson][1]);
        for (int i = 1; i <= k; ++i) {
            maxx = max(maxx,dp[rson][i]);
            dp[node][i] = max(dp[node][i],dp[rson][i - 1] + a[i]);
        }
    }
    dp[node][0] = maxx;
    for (int i = 0; i <= k; ++i) {
        for (int j = 1; i + j + 1<= k; ++j) {
            dp[node][i + j + 1] = max(dp[node][i + j + 1],dp[lson][i] + dp[rson][j] + a[node]);
        }
    }
}
void solve(){
    cin >> k >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    dfs(1);
    for (int i = 0; i <= k; ++i) {
        res = max(res,dp[1][i]);
    }
    cout << res << endl;
}
signed main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
}



















