Help me in solving PARTSCORE problem

My issue

why is there a segmentation fault here?

My code

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

int main() {
	// your code goes here
    int t;
    scanf("%d",&t);
    while(t){
        int n,k;
        
        scanf("%d %d",&n,&k);
        int arr[n];
        
        for (int i=0;i<n;++i){
            scanf("%d",arr[i]);
        }
        sort(arr,arr+n);
        
        if(k==1) printf("%d \n",arr[n-1]*2+arr[0]+arr[n-2]);
        
        else printf("%d \n",arr[n-1]+arr[n-1-k]+arr[0]+arr[n-2]);
        t--;
    }
}

Problem Link: Partition Score Practice Coding Problem