Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 343 ms):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.nio.channels.Pipe.SourceChannel;
import java.rmi.StubNotFoundException;
import java.util.*;

public class cf {
    static int mod = (int) 1e9 + 7;
    static int N = (int) 2e5 + 5;
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int count = 0;

    public static void main(String[] args) {

        // boolean[] isPrime=prime(1000000009);

        int tCase = sc.nextInt();

        l1: 
        while (tCase-- > 0) {
 
            int n=sc.nextInt();
            int l=sc.nextInt();
            int r=sc.nextInt();
 
            List<Integer> lft=new ArrayList<>();
            List<Integer> rght=new ArrayList<>();
            List<Integer> arr=new ArrayList<>();
            long sum=0,ans=0;
            for(int i=0 ;i<n ;i++){
                int temp=sc.nextInt();
                if(i>=l-1 && i<=r-1){
                    
                    arr.add(temp);
                    sum+=(long)temp;
                }else if(i<l){
                    lft.add(temp);
                }else{
                    rght.add(temp);
                }
            }
            long tempSum=sum;
            Collections.sort(arr);
            Collections.sort(lft);
            Collections.sort(rght);

            ans=sum;
            for(int i=0, j=arr.size()-1; i<lft.size() && j>=0; i++,j--){
                if(arr.get(j)>lft.get(i)){
                    sum-=(long)arr.get(j);
                    sum+=(long)lft.get(i);
                }else break;
            }
            ans=Math.min(ans,sum);
            sum=tempSum;
            for(int i=0, j=arr.size()-1; i<rght.size() && j>=0; i++,j--){
                if(arr.get(j)>rght.get(i)){
                    sum-=(long)arr.get(j);
                    sum+=(long)rght.get(i);
                }else break;
            }
            ans=Math.min(ans,sum);
 
            
            
            System.out.println(ans);
            
            

        }
        out.flush();

    }

    public static long findSum(List<Long> l,boolean flag){
        long sum=0;
        for(Long i:l){
            if(flag){
                sum-=i;
            }else{
                sum+=i;
            }

        }
        return Math.abs(sum);
    }

    public static long longSqrt(long n){
        if(n==0){
            return 0;
        }
        if(n==1){
            return 1;
        }
        return BigInteger.valueOf(n).sqrt().longValue();
    }

    public static long fact(int num) {
        if (num == 1 || num == 0)
            return 1;
        return (num  * fact(num - 1));
    }

    public static long powerMod(long base, long exp, long mod) {
        long res = 1;
        base = base % mod;
        if (base == 0)
            return 0;

        while (exp > 0) {
            if ((exp & 1) == 1) {
                res = (res * base) % mod;
            }
            exp = exp >> 1;
            base = (base * base) % mod;
        }
        return res;
    }

    // public static HashMap<String,Pair> sortMap(HashMap<String,Pair> m){
    // List<Map.Entry<String, Pair>> list = new ArrayList<>(m.entrySet());

    // list.sort((e1,e2) -> {
    // Pair p1=e1.getValue();
    // Pair p2=e2.getValue();

    // if (p1.i != p2.i) {
    // return Integer.compare(p1.i, p2.i);
    // } else {
    // return Integer.compare(p1.j, p2.j);
    // }
    // });

    // LinkedHashMap<String, Pair> sortedMap = new LinkedHashMap<>();
    // for (Map.Entry<String, Pair> entry : list) {
    // sortedMap.put(entry.getKey(), entry.getValue());
    // }
    // return sortedMap;
    // }

    public static <K> HashMap<K, Pair> sortMap(Map<K, Pair> map) {
        List<Map.Entry<K, Pair>> list = new ArrayList<>(map.entrySet());

        list.sort((e1, e2) -> {
            Pair p1 = e1.getValue();
            Pair p2 = e2.getValue();

            if (p1.i != p2.i) {
                return Integer.compare(p1.i, p2.i);
            } else {
                return Integer.compare(p1.j, p2.j);
            }
        });

        HashMap<K, Pair> sortedMap = new LinkedHashMap<>();
        for (Map.Entry<K, Pair> entry : list) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        return sortedMap;
    }

    public static long sumOf2(int x, int y) {
        return (long) ((long) Math.pow(2, y + 1) - Math.pow(2, x));
    }

    static class Pair {
        int i;
        int j;

        public Pair(int i, int j) {
            this.i = i;
            this.j = j;
        }
    }

    public static BigDecimal sqrt(BigDecimal value, MathContext mc) {
        BigDecimal x = new BigDecimal(Math.sqrt(value.doubleValue()), mc);
        return x.add(new BigDecimal(value.subtract(x.multiply(x)).doubleValue() / (x.doubleValue() * 2.0), mc));
    }

    

    public static BigDecimal distance(BigDecimal x1, BigDecimal y1, BigDecimal x2, BigDecimal y2) {
        BigDecimal deltaX = x2.subtract(x1);
        BigDecimal deltaY = y2.subtract(y1);

        BigDecimal sumOfSquares = deltaX.multiply(deltaX).add(deltaY.multiply(deltaY));
        return sqrt(sumOfSquares, MathContext.DECIMAL128);
    }

    public static int closestMultiple(int a, int b, int k) {
        int quotient = b / a;
        int multipleBelow = a * quotient;
        int multipleAbove = a * (quotient + 1);

        int distanceBelow = Math.abs(multipleBelow - b);
        int distanceAbove = Math.abs(multipleAbove - b);
        if (distanceBelow <= k && distanceAbove <= k) {
            return distanceBelow < distanceAbove ? multipleBelow : multipleAbove;
        } else if (distanceBelow <= k) {
            return multipleBelow;
        } else if (distanceAbove <= k) {
            return multipleAbove;
        } else {
            return -1;
        }
    }

    public static boolean[] prime(int limit) {
        boolean[] isPrime = new boolean[limit + 1];
        Arrays.fill(isPrime, true);
        isPrime[0] = false;
        isPrime[1] = false;

        for (int i = 2; i * i <= limit; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j <= limit; j += i) {
                    isPrime[j] = false;
                }
            }
        }
        return isPrime;

    }

    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        if (num <= 3) {
            return true;
        }
        if (num % 2 == 0 || num % 3 == 0) {
            return false;
        }
        for (int i = 5; i * i <= num; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }

    public static int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    // public static long combination(int n, int k) {
    // if(n < k) return 0;
    // return (fact[n] * p((fact[n - k] * fact[k]) % mod, mod - 2)) % mod;
    // }
    public static long p(long a, long b) {
        long r = 1;
        while (b > 0) {
            long res = b & 1;
            if (res == 1)
                r = (r * a) % mod;
            b /= 2;
            a = (a * a) % mod;
        }
        return r;
    }

    public static long combination(int n, int k) {
        if (k > n) {
            return 0;
        }
        if (k == 0 || k == n) {
            return 1;
        }
        k = Math.min(k, n - k);
        long c = 1;
        for (int i = 0; i < k; i++) {
            c = c * (n - i) / (i + 1);
        }
        return c;
    }

    public static int findGCD(int[] arr) {
        int result = arr[0];
        for (int i = 1; i < arr.length; i++) {
            result = gcd(result, arr[i]);
            if (result == 1) {
                return 1;
            }
        }
        return result;
    }

    static 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 {
                if (st.hasMoreTokens()) {
                    str = st.nextToken("\n");
                } else {
                    str = br.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

}


Solution 2 (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");
        }
    }
}