Doubt about ZCO 160001

Why wont this logic work? I get some wrong answers. The approach is same as Editorial…
//ignore the time complexity, wht is the problem with my code.
int main() {

ll n,k; int l=0;
cin>>n>>k;
ll A[n],B[n],ans=LLONG_MAX;
for(ll i=0;i<n;i++)
{
    cin>>A[i];
}
sort(A,A+n);
for(ll i=0;i<n;i++)
{
    cin>>B[i];
}
sort(B,B+n);
if(B[n-1]>A[n-1])
l=1;
ans=min(ans,A[n-1]+B[n-1]);
while(k--)
{
    if(l==1)
    {
        ll temp=A[n-1];A[n-1]=B[0];B[0]=temp;
        sort(A,A+n);
        sort(B,B+n);
        ans=min(ans,A[n-1]+B[n-1]);
        
    }
    else
    {
        ll temp=B[n-1];B[n-1]=A[0];A[0]=temp;
        sort(A,A+n);
        sort(B,B+n);
        ans=min(ans,A[n-1]+B[n-1]);
        
    }
}
cout<<ans;
return 0;

}

Care to post this in the editorial comments section? It would help reduce the clutter on discuss. Also, the editorialist or someone reading the editorial might surely help you out.