Language: C++

#include<iostream>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;

int n;
string s;

int a[3011],b[3011];

int begin;
string ans="";
vector<int>r;
string tmp;
int main()
{
	int i,j,k;
	cin>>n>>s;
	int xx,yy,zz;
	
	xx=yy=0;
	for(i=0;i<2*n;i++)
	{
		if(s[i]=='a')
		{
			a[xx++]=i;
		}
		else
		{
			b[yy++]=i;
		}
	}
	
	bool hasba=false;
	begin=2*n;
	for(i=n-1;i>=0;i--)
	{
		if(a[i]<b[i])
		{
			if(b[i]<begin && hasba==false)
			{
				ans+="ab";
				begin=a[i];
				r.insert(r.begin(),b[i]);
				r.insert(r.begin(),a[i]);
			}
		}
		else
		{
			tmp=ans;
			
			for(j=0;j<r.size();j++)
			{
				if(r[j]>a[i])
				{
					tmp.insert(tmp.begin()+j,'a');
					break;
				}
			}
			if(j==r.size())
			{
				tmp.insert(tmp.end(),'a');
			}
			tmp.insert(tmp.begin(),'b');
			/*
			cout<<i<<' '<<tmp<<endl;
			for(j=0;j<r.size();j++)
			{
				cout<<r[j]<<' ';
			}
			cout<<" RRRR"<<endl;
			*/
			
			if(tmp>ans)
			{
				ans=tmp;
				for(j=0;j<r.size();j++)
				{
					if(r[j]>a[i])
					{
						r.insert(r.begin()+j,a[i]);
						break;
					}
				}
				if(j==r.size())
				{
					r.insert(r.end(),a[i]);
				}
				r.insert(r.begin(),b[i]);
			}
			hasba=true;
		}
	}
	
	cout<<ans<<endl;
	return 0;
} 

Compiler output:
a.cc: In function 'int main()':
a.cc:38:9: error: reference to 'begin' is ambiguous
   38 |         begin=2*n;
      |         ^~~~~
In file included from /usr/include/c++/14/string:53,
                 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/range_access.h:114:37: note: candidates are: 'template<class _Tp> const _Tp* std::begin(const valarray<_Tp>&)'
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
/usr/include/c++/14/bits/range_access.h:113:31: note:                 'template<class _Tp> _Tp* std::begin(valarray<_Tp>&)'
  113 |   template<typename _Tp> _Tp* begin(valarray<_Tp>&) noexcept;
      |                               ^~~~~
/usr/include/c++/14/bits/range_access.h:95:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   95 |     begin(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&)'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:52:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&)'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
In file included from /usr/include/c++/14/bits/range_access.h:36:
/usr/include/c++/14/initializer_list:88:5: note:                 'template<class _Tp> constexpr const _Tp* std::begin(initializer_list<_Tp>)'
   88 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
a.cc:14:5: note:                 'int begin'
   14 | int begin;
      |     ^~~~~
a.cc:43:33: error: reference to 'begin' is ambiguous
   43 |                         if(b[i]<begin && hasba==false)
      |                                 ^~~~~
/usr/include/c++/14/bits/range_access.h:114:37: note: candidates are: 'template<class _Tp> const _Tp* std::begin(const valarray<_Tp>&)'
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
/usr/include/c++/14/bits/range_access.h:113:31: note:                 'template<class _Tp> _Tp* std::begin(valarray<_Tp>&)'
  113 |   template<typename _Tp> _Tp* begin(valarray<_Tp>&) noexcept;
      |                               ^~~~~
/usr/include/c++/14/bits/range_access.h:95:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   95 |     begin(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&)'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:52:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&)'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/initializer_list:88:5: note:                 'template<class _Tp> constexpr const _Tp* std::begin(initializer_list<_Tp>)'
   88 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
a.cc:14:5: note:                 'int begin'
   14 | int begin;
      |     ^~~~~
a.cc:46:33: error: reference to 'begin' is ambiguous
   46 |                                 begin=a[i];
      |                                 ^~~~~
/usr/include/c++/14/bits/range_access.h:114:37: note: candidates are: 'template<class _Tp> const _Tp* std::begin(const valarray<_Tp>&)'
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
/usr/include/c++/14/bits/range_access.h:113:31: note:                 'template<class _Tp> _Tp* std::begin(valarray<_Tp>&)'
  113 |   template<typename _Tp> _Tp* begin(valarray<_Tp>&) noexcept;
      |                               ^~~~~
/usr/include/c++/14/bits/range_access.h:95:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   95 |     begin(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&)'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:52:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&)'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/initializer_list:88:5: note:                 'template<class _Tp> constexpr const _Tp* std::begin(initializer_list<_Tp>)'
   88 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
a.cc:14:5: note:                 'int begin'
   14 | int begin;
      |     ^~~~~