Language: C++

#include <iostream>
#include <set>
#include <cstring>
using namespace std;

#define MAXN 1024
#define MAXV 512
#define MAXW 20

short int dp[MAXW][MAXN][MAXN], weight[512];

int _dp(int w, int a, int b) {
    if (a == b) dp[w][a][b] = 0;
    else if (dp[w][a][b] == -1) {
//        cout << "considerando w = " << w << "; a = " << a << "; b = " << b << endl;
        dp[w][a][b] = max(_dp(w, a+1, b), _dp(w, a, b-1));
        if (w) for (int i=a+1; i<b; i++) if (_dp(w-1, a, i) && _dp(w-1, i, b)) dp[w][a][b] = max(dp[w][a][b], _dp(w-1, a, i)+_dp(w-1, i, b));
    }
    return dp[w][a][b];
}

int answer(int m[], int M) {
    int RES = 0, i, j;

//    for (i=0; i<M; i++) cout << m[i] << ' '; cout << endl;

    memset(dp, -1, sizeof(dp));
    for (i=0; i<M; i++) dp[weight[m[i]]][i][i+1] = 1;
    for (i=0; i<MAXW; i++) RES = max(RES, _dp(i, 0, M));
    return RES;
}

int main() {
    int N, n[MAXN], m[MAXN], i, M, BEST;
    bool skip[MAXV];
    set<int> v, vcur;

    weight[1] = 0;
    for (i=2; i<MAXV; i*=2) weight[i] = weight[i/2]+1;

    while (cin >> N && N) {
        v.clear();
        for (i=0; i<N; i++) {
            cin >> n[i];
            v.insert(n[i]);
        }
    
        memset(skip, false, sizeof(skip)); BEST = 0;
        for (set<int>::iterator it=v.begin(); it!= v.end(); it++) if (!skip[*it]) {
            vcur.clear(); i = *it;
            do {
                vcur.insert(i);
                skip[i] = true;
                i *= 2;
            } while (i <= MAXV);
    
            M = 0;
            for (i=0; i<N; i++) if (vcur.find(n[i]) != vcur.end()) m[M++] = n[i]/(*it);
    
            BEST = max(BEST, answer(m, M));
        }
        cout << BEST << endl;
    }

    return 0;
}

Compiler output:
a.cc: In function 'int _dp(int, int, int)':
a.cc:17:97: error: no matching function for call to 'max(short int&, int)'
   17 |         if (w) for (int i=a+1; i<b; i++) if (_dp(w-1, a, i) && _dp(w-1, i, b)) dp[w][a][b] = max(dp[w][a][b], _dp(w-1, a, i)+_dp(w-1, i, b));
      |                                                                                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:17:97: note:   deduced conflicting types for parameter 'const _Tp' ('short int' and 'int')
   17 |         if (w) for (int i=a+1; i<b; i++) if (_dp(w-1, a, i) && _dp(w-1, i, b)) dp[w][a][b] = max(dp[w][a][b], _dp(w-1, a, i)+_dp(w-1, i, b));
      |                                                                                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided