Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 217 ms):
  
/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {

    //------------------------------------------main--------------------------------------------------//
    public static void main(String[] args) throws java.lang.Exception {
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        FastReader sc = new FastReader();
        Task s = new Task(sc, out);
        s.solve();
        out.close();
        // your code goes here"
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public int[] readIntArray(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    public long[] readLongArray(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }
}

class Task {

    PrintWriter out;
    FastReader sc;

    Task(FastReader sc, PrintWriter out) {
        this.sc = sc;
        this.out = out;
    }
    //----------------------------------------------inputfunction------------------------------------------//

    String st() {
        return sc.nextLine();
    }

    int i() {
        return sc.nextInt();
    }

    long l() {
        return sc.nextLong();
    }

    double d() {
        return sc.nextDouble();
    }

    //---------------------------------------------absolute----------------------------------------------//
    public long abs(long a) {
        return a < 0 ? -a : a;
    }

    public int abs(int a) {
        return a < 0 ? -a : a;
    }

    //-------------------------------------------max--------------------------------------------------//
    public int max(int a, int b) {
        return a >= b ? a : b;
    }

    public long max(long a, long b) {
        return a >= b ? a : b;
    }

    public int max(int a, int b, int c) {
        return Math.max(a, Math.max(b, c));
    }

    public long max(long a, long b, long c) {
        return Math.max(a, Math.max(b, c));
    }

    //------------------------------------------min---------------------------------------------------//
    public int min(int a, int b) {
        return a <= b ? a : b;
    }

    public long min(long a, long b) {
        return a <= b ? a : b;
    }

    public int min(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }

    public long min(long a, long b, long c) {
        return Math.min(a, Math.min(b, c));
    }

    //----------------------------------------sortingfunction---------------------------------------------//
    private void sort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }
    //---------------------------------------power----------------------------------------------------//

    public long pow(long a, int b) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans *= a;
            }
            a *= a;
            b >>= 1;
        }
        return b;
    }
    //----------------------------------------binary_exponentiation-----------------------------------//

    public long exp(long a, long b, long mod) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans = (ans * a) % mod;
            }
            a = (a * a) % mod;
            b >>= 1;
        }
        return ans;
    }

    //---------------------------------------helpingmethods-------------------------------------------//
    //---------------------------------------main-----------------------------------------------------//
    int t, n;

    public void solve() throws IOException {
        int T = sc.nextInt();
        for(int  t= 1;t<=T;t++) {
            int n = i(), l  = i(),  r= i();
            List<Integer> x = new ArrayList<>(), y = new ArrayList<>(), z = new ArrayList<>();
            for(int i= 1;i<l;i++) x.add(i());
            for(int i  = l ;i<=r;i++) y.add(i());
            for(int i = r+1;i<=n;i++) z.add(i());
            long ans = 0, sum= 0;
            Collections.sort(x);
            Collections.sort(y);
            Collections.sort(z);
            Collections.reverse(y);
            for(int e: y) sum += e;
            long temp = ans = sum;
            for(int i = 0;i<min(y.size(), x.size());i++){
                if(x.get(i)>=y.get(i)) break;
                temp += x.get(i) - y.get(i);
            }
            ans = min(ans, temp);
            temp= sum;
            for(int i = 0;i<min(y.size(), z.size());i++){
                if(z.get(i)>=y.get(i)) break;
                temp += z.get(i) - y.get(i);
            }
            ans = min(temp , ans );
            out.print(ans);
            out.print("\n");
        }
    }
}


Solution 2 (runtime: 452 ms):
import java.util.Scanner;
public class Subsequence_Update{
    public static void merge(int[] arr, int l, int m, int r){
        int n1 = m - l + 1;
        int n2 = r - m;

        int[] L = new int[n1];
        int[] R = new int[n2];

        for(int i = 0; i<n1; i++){
            L[i] = arr[l + i];
        }

        for(int i = 0; i<n2; i++){
            R[i] = arr[m + 1 + i];
        }

        int i = 0;
        int j = 0;
        int k = l;

        while(i<n1 && j<n2){
            if(L[i] <= R[j]){
                arr[k] = L[i];
                i++;
            }else{
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        while(i<n1){
            arr[k] = L[i];
            i++;
            k++;
        }

        while(j<n2){
            arr[k] = R[j];
            j++;
            k++;
        }
    }
    public static void mergeSort(int[] arr, int l, int r){
        if(l<r){
            int m = (l+r)/2;
            mergeSort(arr, l, m);
            mergeSort(arr, m+1, r);
            merge(arr, l, m, r);
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Long t = sc.nextLong();
        for(int i = 0; i<t; i++){
            int n = sc.nextInt();
            long l = sc.nextLong();
            long r = sc.nextLong();
            int[] arr = new int[n];
            for(int j = 0; j<n; j++){
                arr[j] = sc.nextInt();
            }

            int[] arr1 = new int[(int) r];
            for(long j = 0; j<r; j++){
                arr1[(int)j] = arr[(int)j];
            }

            int[] arr2 = new int[(int)(n-l + 1)];
            for(int j = 0; j<n-l+1; j++){
                arr2[(int)j] = arr[(int)(j+l-1)];
            }

            mergeSort(arr1, 0, (int)(r-1));
            mergeSort(arr2, 0, (int)(n-l));

            Long sum1 = 0l;
            Long sum2 = 0l;

            for(int j = 0; j<(r - l + 1); j++){
                sum1 += arr1[j];
                sum2 += arr2[j];
            }

            if(sum1<=sum2){
                System.out.println(sum1);
            }else{
                System.out.println(sum2);
            }
        }
    }
}