Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 327 ms):
import java.io.*;

import java.util.*;



public class Main {

    public static void main(String[] args) throws IOException {



        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));



        if (System.getProperty("ONLINE_JUDGE") == null) {

            br = new BufferedReader(new FileReader("src/Input.txt"));

            bw = new BufferedWriter(new FileWriter("src/Output.txt"));

        }



        // Your code here



        int t = Integer.parseInt(br.readLine());

        while(t --> 0) {

            StringTokenizer st = new StringTokenizer(br.readLine());

            int n = Integer.parseInt(st.nextToken());

            int l = Integer.parseInt(st.nextToken())-1;

            int r = Integer.parseInt(st.nextToken())-1;



            long[] arr =new long[n];

            st = new StringTokenizer(br.readLine());

            for(int i=0; i<n; i++){

                arr[i] = Long.parseLong(st.nextToken());

            }



            // [0,r] [l,n-1]

            long[] left = Arrays.copyOfRange(arr, 0, r+1);

            long[] right = Arrays.copyOfRange(arr, l, n);



            Arrays.sort(left);

            Arrays.sort(right);



            long leftSum = 0;

            long rightSum = 0;

            int k = r-l+1;

            for(int i=0;i <k; i++) {

                leftSum += left[i];

            }

            for(int i=0;i <k; i++) {

                rightSum += right[i];

            }



            bw.write(Math.min(leftSum, rightSum) + "\n");

        }



        bw.flush();

        br.close();

        bw.close();

    }

}

Solution 2 (runtime: 577 ms):
import java.util.*;



public class Main {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        

        int t = sc.nextInt();



        while(t-- > 0) {

            solve();

        }

    }



    static void solve() {

        int n = sc.nextInt();

        int l = sc.nextInt() - 1;

        int r = sc.nextInt() - 1;



        int[] a = new int[n];



        for (int i = 0 ; i < n ; i++) a[i] = sc.nextInt();



        Queue<Integer> before = new PriorityQueue<>();

        Queue<Integer> after = new PriorityQueue<>();

        Queue<Integer> res1 = new PriorityQueue<>(Collections.reverseOrder());

        Queue<Integer> res2 = new PriorityQueue<>(Collections.reverseOrder());



        for (int i = 0 ; i < n ; i++) {

            if (i < l) before.add(a[i]);

            else if (i > r) after.add(a[i]);

            else {

                res1.add(a[i]);

                res2.add(a[i]);

            }

        }



        while(!before.isEmpty() && before.peek() < res1.peek()) {

            res1.add(before.poll());

            res1.poll();

        }



        long  sum = 0;

        while(!res1.isEmpty()) sum += res1.poll();



        while(!after.isEmpty() && after.peek() < res2.peek()) {

            res2.add(after.poll());

            res2.poll();

        }

        long sum2 = 0;

        while(!res2.isEmpty()) sum2 += res2.poll();



        System.out.println(Math.min(sum, sum2));

    }

}