Code not working Can you plz help me resolve it

#include
#include
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
for (int i = 1; i <n; i++)
{
string S1=to_string(arr[0]);
string S2=to_string(arr[i]);
if ((S1+S2)>(S2+S1))
{
string s=S1+S2;
int ans=stoi(s);
arr[0]=ans;
}
else{
string s=S2+S1;
int ans=stoi(s);
arr[0]=ans;
}
}
cout<<arr[0];
}

@pratham.sharma6113 your are missing that we can place any number anywhere we wants you are considring them seqentially and your code fails
for example
1 34 3 98 9 76
for code will create the result by below seps
341 3 98 9 76
3413 98 9 76
983413 9 76
9983413 76
998341376
but this is not the optimal reuslt you can use 76 before and then 1 34 4 later
the no becomes
998763431

second you are using stoi in your code which causes overflow when number becomes greater than integer range do not make it intger again
Coding Blocks IDE
this is the implemented code here i sort the array using the same logic as your are using for comparing the two string but here we consider all the cases that each element can be used anwhere and its initial ordering do not matter
(sorry for spelling mistakes)
if issue still exists let me know and if clear rate my experience

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.