Language: C++

#include<stdio.h>
#include<algorithm>
using namespace std;
int b[1100];
int dp[510][1010];
int next[1010];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<a;i++)scanf("%d",b+i);
		int ret=1;
		for(int i=0;i<510;i++)for(int j=0;j<=a;j++)
			dp[i][j]=0;
		for(int i=0;i<a;i++)dp[b[i]][i]=1;
		for(int i=0;i<a;i++){
			next[i]=-1;
			for(int j=i+1;j<a;j++){
				bool ok=true;
				int S=b[i];
				int T=b[j];
				if(S>T)swap(S,T);
				int U=T/S;
				if(T%S)ok=false;
				while(U%2==0)U/=2;
				if(U!=1)ok=false;
				if(ok){
					next[i]=j;break;
				}
			}
		}
	//	for(int i=0;i<a;i++)printf("%d ",next[i]);
		int sum=0;
		for(int i=0;i<a;i++)sum+=b[i];
		for(int i=0;i<=sum;i++){
			int t=i%505;
			int nt=(t+501)%505;
			for(int j=0;j<=a;j++)dp[nt][j]=0;
			for(int j=0;j<a;j++){
				if(!dp[t][j])continue;
				if(~next[j]&&i%b[next[j]]==0){
					dp[(t+b[next[j]])%505][next[j]]=max(dp[(t+b[next[j]])%505][next[j]],dp[t][j]+1);
				}
				if(~next[j]){
					dp[t][next[j]]=max(dp[t][next[j]],dp[t][j]);
				}
			}
			for(int j=0;j<a;j++){
				if(ret>=dp[t][j]||i%b[j])continue;
				int q=i/b[j];
				while(q%2==0)q/=2;
				if(q==1)ret=dp[t][j];
			}
		}
		printf("%d\n",ret);
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:16:25: error: reference to 'next' is ambiguous
   16 |                         next[i]=-1;
      |                         ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:27:41: error: reference to 'next' is ambiguous
   27 |                                         next[i]=j;break;
      |                                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:40:37: error: reference to 'next' is ambiguous
   40 |                                 if(~next[j]&&i%b[next[j]]==0){
      |                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:40:50: error: reference to 'next' is ambiguous
   40 |                                 if(~next[j]&&i%b[next[j]]==0){
      |                                                  ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:41:49: error: reference to 'next' is ambiguous
   41 |                                         dp[(t+b[next[j]])%505][next[j]]=max(dp[(t+b[next[j]])%505][next[j]],dp[t][j]+1);
      |                                                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:41:64: error: reference to 'next' is ambiguous
   41 |                                         dp[(t+b[next[j]])%505][next[j]]=max(dp[(t+b[next[j]])%505][next[j]],dp[t][j]+1);
      |                                                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:41:85: error: reference to 'next' is ambiguous
   41 |                                         dp[(t+b[next[j]])%505][next[j]]=max(dp[(t+b[next[j]])%505][next[j]],dp[t][j]+1);
      |                                                                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:41:100: error: reference to 'next' is ambiguous
   41 |                                         dp[(t+b[next[j]])%505][next[j]]=max(dp[(t+b[next[j]])%505][next[j]],dp[t][j]+1);
      |                                                                                                    ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:43:37: error: reference to 'next' is ambiguous
   43 |                                 if(~next[j]){
      |                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:44:47: error: reference to 'next' is ambiguous
   44 |                                         dp[t][next[j]]=max(dp[t][next[j]],dp[t][j]);
      |                                               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~
a.cc:44:66: error: reference to 'next' is ambiguous
   44 |                                         dp[t][next[j]]=max(dp[t][next[j]],dp[t][j]);
      |                                                                  ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:6:5: note:                 'int next [1010]'
    6 | int next[1010];
      |     ^~~~