Getting some WA test results in Bookshelves problem.

Here is my code for the BookShelves problem:

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n, k, maxa, maxb;
    scanf("%d %d", &n, &k);
    int a[n], b[n];
    
    for(int i=0;i<n;i++) scanf("%d", &a[i]);
    sort(a, a+n);
    
    for(int i=0;i<n;i++) scanf("%d", &b[i]);
    sort(b, b+n);
    
    maxb = b[n-1], maxa = a[n-1];
    
    if(b[n-1]>a[n-1]) for(int t = 1;t<=k;t++) if(a[n-t] < b[t-1]) break; else maxa = a[n-t-1];
    else for(int t = 1;t<=k;t++) if(b[n-t] < a[t-1]) break; else maxb = b[n-t-1];
    
    printf("%d\n",maxa+maxb);
    
    return 0;
}

Where is this code breaking?

Try this case:

5 2 20 1 2 3 4 5 6 7 8 9

The answer should be 25. The tricky bit finding that value of 5.