Problem: 2063B - Subsequence Update
Language: Java 8

Previous submission (verdict: Wrong answer on test 1):
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
            int[] arr = new int[n + 1];  // Using n+1 to handle 1-based indexing
            for (int i = 1; i <= n; i++) {
                arr[i] = sc.nextInt();
            }

            int max = 0;

            // Step 1: Calculate the sum for the range [l, r]
            for (int i = l; i <= r; i++) {
                max += arr[i];
            }

            // Step 2: Use priority queue to store the elements in the range [1, r] 
            PriorityQueue<Integer> pq = new PriorityQueue<>();
            for (int i = 1; i <= r; i++) {
                pq.add(arr[i]);
            }

            int sum = 0;
            int tl = l;
            int tr = r;

            // Step 3: Add elements to sum from the priority queue and calculate the new range
            while (tr - tl + 1 > 0) {
                sum += pq.poll();  // Poll the minimum element
                tl++;  // Increment the left boundary
            }

            max = Math.max(max, sum);  // Track the maximum sum obtained
            pq.clear();

            // Step 4: Add all elements from l to n into the priority queue
            for (int i = l; i <= n; i++) {
                pq.add(arr[i]);
            }

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


Submission (verdict: Wrong answer on test 8):
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
            int[] arr = new int[n + 1];  // Using n+1 to handle 1-based indexing
            for (int i = 1; i <= n; i++) {
                arr[i] = sc.nextInt();
            }

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

            // Step 2: Find the smallest `r - l + 1` elements from [1, r]
            List<Integer> prefix = new ArrayList<>();
            for (int i = 1; i <= r; i++) {
                prefix.add(arr[i]);
            }
            Collections.sort(prefix);
            int 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<Integer> suffix = new ArrayList<>();
            for (int i = l; i <= n; i++) {
                suffix.add(arr[i]);
            }
            Collections.sort(suffix);
            int 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);
        }
    }
}