Split array into K non-empty subsets such that sum of their maximums and minimums is maximized

import java.io.*;import java.lang.*;import java.util.*;class GFG{static int maximumSum(int arr[], int S[],                      int N, int K){             int ans = 0;            Arrays.sort(arr);    for(int i = 0; i < N / 2; i++)    {        int temp = arr[i];        arr[i] = arr[N - 1 - i];        arr[N - 1 - i] = temp;    }        for(int i = 0; i < K; i++)        ans += arr[i];            Arrays.sort(S);        for(int i = 0; i < K; i++)    {                         if (S[i] == 1)            ans += arr[i];        S[i]--;    }            int counter = K - 1;        for(int i = 0; i < K; i++)    {                counter = counter + S[i];        if (S[i] != 0)            ans += arr[counter];    }        return ans;}public static void main(String[] args){    int arr[] = { 1, 13, 7, 17 };    int S[] = { 1, 3 };    int N = arr.length;    int K = S.length;         System.out.println(maximumSum(arr, S, N, K));}}