Language: C++

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

const int inf=1e6;

int a[1010];
int N;

vector<int> vals;
int dp[1010][257];

struct edge{
	int to,cost;
	edge(){}
	edge(int a,int b):to(a),cost(b){}
};

//vector<edge> G[1010];
int dp2[1010][1010];
edge G[1010*1010];
int sz[1010];
int c=0;

bool check(int x){
	while(x%2==0) x/=2;
	return x==1;
}

int put(int mi){
	int res=0;
	c=0;
	vals.clear();
	for(int i=0;i<N;i++){
		if(a[i]%mi==0&&check(a[i]/mi)) vals.push_back(a[i]/mi);
	}
	int n=vals.size();
	if(n==0) return 0;
//	for(int i=0;i<n;i++) printf("%d ",vals[i]);
//	printf("\n");
	for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) dp2[i][j]=0;
	for(int le=0;le<n;le++){
		for(int i=le;i<=n;i++) for(int j=0;j<=256;j++) dp[i][j]=-inf;
		dp[le][0]=0;
		int prev=-1;
		sz[le]=0;
		for(int ri=le;ri<n;ri++){
			for(int j=0;j<256;j++){
				if(dp[ri][j]==-inf) continue;
				int ac=j&(-j);
				if(vals[ri]<=ac||j==0){
					dp[ri+1][j+vals[ri]]=max(dp[ri+1][j+vals[ri]],dp[ri][j]+1);
					if(j+vals[ri]==256&&dp[ri+1][256]>prev){
						prev=dp[ri][256];
						sz[le]++;
						G[c++]=edge(ri,dp[ri][256]);
				}
				dp[ri+1][j]=max(dp[ri+1][j],dp[ri][j]);
			}
		}
//		for(int j=0;j<=8;j++) printf("%d ",dp[n][j]);
//		printf("\n");
		for(int j=1;j<=256;j*=2){
			res=max(res,dp[n][j]);
		}
	//	G[le].clear();
	/*	int prev=-1;
		sz[le]=0;
		for(int ri=le+1;ri<=n;ri++){
			if(dp[ri][256]==-inf) continue;
			if(prev<dp[ri][256]){
			//	G[le].push_back(edge(ri,dp[ri][256]));
				prev=dp[ri][256];
				sz[le]++;
				G[c++]=edge(ri,dp[ri][256]);
			}
		//	if(c>1000*1000) fprintf(stderr,"error:c\n");
		}*/
	}
	for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) dp2[i][j]=-inf;
	dp2[0][0]=0;
	int id=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<=n;j++){
		//	for(int k=0;k<G[i].size();k++){
			for(int k=0;k<sz[i];k++){
				int to=G[id+k].to,cost=G[id+k].cost;
		//		if(id+k>1000*1000) fprintf(stderr,"error:id\n");
				dp2[to][j+1]=max(dp2[to][j+1],dp2[i][j]+cost);
			}
			dp2[i+1][j]=max(dp2[i+1][j],dp2[i][j]);
		//	id+=sz[i];
		}
		id+=sz[i];
	}
	for(int i=1;i<=n;i*=2) res=max(res,dp2[n][i]);
	return res;
}

int solve(){
	int res=0;
	for(int i=1;i<=500;i+=2){
		int tmp=put(i);
		res=max(res,tmp);
	}
	return res;
}

int main(){
	while(true){
		scanf("%d",&N);
		if(N==0) break;
		for(int i=0;i<N;i++) scanf("%d",a+i);
		int ans=solve();
		printf("%d\n",ans);
	}
	return 0;
}

Compiler output:
a.cc: In function 'int put(int)':
a.cc:102:10: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  102 | int solve(){
      |          ^~
a.cc:102:10: note: remove parentheses to default-initialize a variable
  102 | int solve(){
      |          ^~
      |          --
a.cc:102:10: note: or replace parentheses with braces to value-initialize a variable
a.cc:102:12: error: a function-definition is not allowed here before '{' token
  102 | int solve(){
      |            ^
a.cc:111:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  111 | int main(){
      |         ^~
a.cc:111:9: note: remove parentheses to default-initialize a variable
  111 | int main(){
      |         ^~
      |         --
a.cc:111:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:111:11: error: a function-definition is not allowed here before '{' token
  111 | int main(){
      |           ^
a.cc:120:2: error: expected '}' at end of input
  120 | }
      |  ^
a.cc:32:16: note: to match this '{'
   32 | int put(int mi){
      |                ^
a.cc:120:2: warning: control reaches end of non-void function [-Wreturn-type]
  120 | }
      |  ^