Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 343 ms):
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();  // number of test cases
        while (t-- > 0) {
            // Read n, l, r
            int n = sc.nextInt();
            int l = sc.nextInt();  // 1-based index
            int r = sc.nextInt();  // 1-based index

            // Read array arr
            long[] arr = new long[n + 1];  // Using long[] for larger values, and n+1 to handle 1-based indexing
            for (int i = 1; i <= n; i++) {
                arr[i] = sc.nextLong();
            }

            // Step 1: Calculate the sum for the range [l, r]
            long initialSum = 0;
            for (int i = l; i <= r; i++) {
                initialSum += arr[i];
            }
            long min = initialSum;  // Store the initial sum as the minimum

            // Step 2: Find the smallest `r - l + 1` elements from [1, r]
            List<Long> prefix = new ArrayList<>();
            for (int i = 1; i <= r; i++) {
                prefix.add(arr[i]);
            }
            Collections.sort(prefix);
            long prefixSum = 0;
            // Sum the smallest `r - l + 1` elements from the sorted prefix
            for (int i = 0; i < r - l + 1; i++) {
                prefixSum += prefix.get(i);
            }
            min = Math.min(min, prefixSum);  // Update the minimum sum

            // Step 3: Find the smallest `r - l + 1` elements from [l, n]
            List<Long> suffix = new ArrayList<>();
            for (int i = l; i <= n; i++) {
                suffix.add(arr[i]);
            }
            Collections.sort(suffix);
            long suffixSum = 0;
            // Sum the smallest `r - l + 1` elements from the sorted suffix
            for (int i = 0; i < r - l + 1; i++) {
                suffixSum += suffix.get(i);
            }
            min = Math.min(min, suffixSum);  // Update the minimum sum

            // Output the result for the current test case
            System.out.println(min);
        }
    }
}


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));

    }

}